httpWebRequest与httpWebResponse 类学习(校内网,开心网登录留言)
今天学习了httpWebRequest与httpWebResponse 类,顺便写了个登录校内网的方法:
登录校内网:
public string GetHtmlNoPostData()
{
HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create("http://login.xiaonei.com/Login.do");
HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
Stream responseStream = httpWebResponse.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);
string html = streamReader.ReadToEnd();
streamReader.Close();
responseStream.Close();
return html;
}
public string GetHtml()
{
/***** 以下代码请求登录页面,并设置CookieContainer保存登录信息 *****/
// postData
byte[] bytePostData = PostData;
// 声明一个CookieContainer
CookieContainer cookieContainer = new CookieContainer();
HttpWebRequest httpLoginRequest =
(HttpWebRequest)HttpWebRequest.Create("http://login.xiaonei.com/Login.do");
// 给HttpWebRequest指定CookieContainer
httpLoginRequest.CookieContainer = cookieContainer;
// 构建即将发送的标头
httpLoginRequest.ContentType = "application/x-www-form-urlencoded";
httpLoginRequest.Accept =
"image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
httpLoginRequest.UserAgent =
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 2.0.50727)";
httpLoginRequest.Method = "POST";
httpLoginRequest.KeepAlive = true;
httpLoginRequest.AllowAutoRedirect = true;
httpLoginRequest.ContentLength = bytePostData.Length;
// 获取用于写入请求数据的 Stream 对象
Stream stream = httpLoginRequest.GetRequestStream();
// 向当前流中写入字节序列
stream.Write(bytePostData, 0, bytePostData.Length);
// 关闭流对象
stream.Close();
//发送请求获取响应内容
HttpWebResponse httpLoginResponse = (HttpWebResponse)httpLoginRequest.GetResponse();
/***** 以上代码请求登录页面,并设置CookieContainer保存登录信息 *****/
/************** 以下代码请求一个需要登录才能访问的页面 **************/
HttpWebRequest httpWebRequest =
(HttpWebRequest)HttpWebRequest.Create("http://friend.xiaonei.com/myfriendlistx.do");
//刚才那个CookieContainer已经存有了Cookie,把它附加到HttpWebRequest中
httpWebRequest.CookieContainer = cookieContainer;
//发送请求获取响应内容
HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
Stream responseStream = httpWebResponse.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);
this.strHtml = streamReader.ReadToEnd();
streamReader.Close();
responseStream.Close();
/************** 以上代码请求一个需要登录才能访问的页面 **************/
}
登录开心网:
private void GetLogin()
{
/***** 以下代码请求登录页面,并设置CookieContainer保存登录信息 *****/
// postData
byte[] bytePostData =
Encoding.Default.GetBytes("url=%2F&email=*****&password=*****");
HttpWebRequest httpLoginRequest =
(HttpWebRequest)HttpWebRequest.Create("http://www.kaixin001.com/login/login.php");
// 给HttpWebRequest指定CookieContainer
httpLoginRequest.CookieContainer = this.cookieContainer;
// 构建即将发送的标头
httpLoginRequest.ContentType = "application/x-www-form-urlencoded";
httpLoginRequest.Accept =
"image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
httpLoginRequest.UserAgent =
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 2.0.50727)";
httpLoginRequest.Method = "POST";
httpLoginRequest.KeepAlive = true;
httpLoginRequest.AllowAutoRedirect = true;
httpLoginRequest.ContentLength = bytePostData.Length;
// 获取用于写入请求数据的 Stream 对象
Stream stream = httpLoginRequest.GetRequestStream();
// 向当前流中写入字节序列
stream.Write(bytePostData, 0, bytePostData.Length);
// 关闭流对象
stream.Close();
//发送请求获取响应内容
HttpWebResponse httpLoginResponse = (HttpWebResponse)httpLoginRequest.GetResponse();
Stream responseStream = httpLoginResponse.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);
this.strHtml = streamReader.ReadToEnd();
streamReader.Close();
responseStream.Close();
/***** 以上代码请求登录页面,并设置CookieContainer保存登录信息 *****/
}
private void Comment()
{
/***** 以下代码发表留言 *****/
// postData
byte[] bytePostData =
Encoding.Default.GetBytes("type=0&id=11282291&ouid=11282291&texttype=html&content=%3CP%3E%E6%96%B0%E5%B9%B4%E5%BF%AB%E4%B9%90%EF%BC%81%3CA%20href%3D%22http%3A%2F%2Fwww.kaixin001.com%2Fhome%2F%3Fuid%3D11282291%22%3E%3C%2FA%3E%3C%2FP%3E&title=&hidden=0&ispwd=0&_=");
HttpWebRequest httpLoginRequest =
(HttpWebRequest)HttpWebRequest.Create("http://www.kaixin001.com/comment/post.php");
// 给HttpWebRequest指定CookieContainer
httpLoginRequest.CookieContainer = this.cookieContainer;
// 构建即将发送的标头
httpLoginRequest.ContentType = "application/x-www-form-urlencoded";
httpLoginRequest.Accept =
"image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
httpLoginRequest.UserAgent =
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 2.0.50727)";
httpLoginRequest.Method = "POST";
httpLoginRequest.KeepAlive = true;
httpLoginRequest.AllowAutoRedirect = true;
httpLoginRequest.ContentLength = bytePostData.Length;
// 获取用于写入请求数据的 Stream 对象
Stream stream = httpLoginRequest.GetRequestStream();
// 向当前流中写入字节序列
stream.Write(bytePostData, 0, bytePostData.Length);
// 关闭流对象
stream.Close();
/***** 以上代码发表留言 *****/
//发送请求获取响应内容
HttpWebResponse httpLoginResponse = (HttpWebResponse)httpLoginRequest.GetResponse();
Stream responseStream = httpLoginResponse.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);
this.strHtml = streamReader.ReadToEnd();
streamReader.Close();
responseStream.Close();
}