|
楼主 |
发表于 2014-5-15 17:24:04
|
显示全部楼层
string func_url(string str)
{
byte[] buffer = Encoding.GetEncoding("UTF-8").GetBytes(str);
String str1 = null;
String str2 = null;
for (int n = 0; n < buffer.Length; n++)
{
str1 = String.Format("%{0:x}", buffer[n]);
str2 = str2 + str1;
}
return str2;
}
private void button5_Click(object sender, EventArgs e)
{
CookieContainer cookie = PostData_douban("http://www.douban.com/accounts/login");
HttpWebRequest HttpWReq = (HttpWebRequest)WebRequest.Create("http://www.douban.com/note/create");
string title = func_url("上海治疗痔疮要多少钱");
string body = func_url("上海治疗痔疮要多少钱,然后我在这个基础上访问其他页面的时候被提示,需要先登陆.");
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] b = encoding.GetBytes(title + body);
HttpWReq.CookieContainer = cookie;
HttpWReq.Host = "www.douban.com";
HttpWReq.Method = "POST";
HttpWReq.UserAgent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)";
HttpWReq.ContentType = "application/x-www-form-urlencoded";
HttpWReq.Accept = "text/html, application/xhtml+xml, */*";
HttpWReq.Headers.Add("Accept-Encoding", "gzip, deflate");
HttpWReq.Headers.Add("DNT", "1");
HttpWReq.ContentLength = b.Length;
//写入数据
Stream stream = HttpWReq.GetRequestStream();
stream.Write(b, 0, b.Length);
stream.Close();
//发送并接收响应
HttpWebResponse HttpWResp = (HttpWebResponse)HttpWReq.GetResponse();
if (HttpWResp.StatusDescription == "OK")
{
//StreamReader reader = new StreamReader(HttpWResp.GetResponseStream(), Encoding.Default);
//string str = reader.ReadToEnd();
//Gzip解压缩
Stream st = HttpWResp.GetResponseStream();
if (HttpWResp.ContentEncoding.ToLower().Contains("gzip"))
{
st = new GZipStream(st, CompressionMode.Decompress);
}
StreamReader sr = new StreamReader(st, Encoding.UTF8);
string s = sr.ReadToEnd();
textBox5.Text = s;
}
HttpWResp.Close();
HttpWReq.Abort();
}
这次所有的代码都在这里了。 |
|