httpWebRequest与httpWebResponse 类学习(校内网,开心网登录留言)
2008-11-29 21:36
603 查看
今天学习了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();
}
登录校内网:
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();
}
相关文章推荐
- c# HttpWebRequest 和HttpWebResponse 登录网站或论坛(校内网登陆)
- c# HttpWebRequest 和HttpWebResponse 登录网站或论坛(校内网登陆)
- C#利用HttpWebRequest、HttpWebResponse调用12306接口,实现登录、查票、买票。
- C#使用HttpWebRequest与HttpWebResponse模拟用户登录
- C#使用HttpWebRequest与HttpWebResponse模拟用户登录
- 利用HttpWebRequest和HttpWebResponse获取Cookie并实现模拟登录
- C# 利用 HttpWebRequest 和 HttpWebResponse 模拟登录有验证码的网站
- [C#]基于HttpWebRequest和HttpWebResponse的自动登录采集
- C# 利用 HttpWebRequest 和 HttpWebResponse 模拟登录有验证码的网站
- C#---HttpWebRequest+HttpWebResponse实现登录人人网
- C# 利用 HttpWebRequest 和 HttpWebResponse 模拟登录有验证码的网站
- 利用HttpWebRequest和HttpWebResponse获取Cookie并实现模拟登录
- 使用HttpWebRequest和HttpWebResponse实现模拟登录需要登陆后才可以访问的页面
- 利用HttpWebRequest和HttpWebResponse获取Cookie并实现模拟登录
- HttpWebRequest的GetResponse或GetRequestStream偶尔超时 + 总结各种超时死掉的可能和相应的
- HttpServletRequest和HttpServletResponse实现登录(附乱码处理代码)(2017.9.28第一次修改版)
- HttpWebRequest的GetResponse或GetRequestStream偶尔超时 + 总结各种超时死掉的可能和相应的解决办法
- 系统地学习ASP.NET AJAX(8) - 客户端脚本编程(Sys.Net命名空间下的WebRequestManager、WebRequest、WebRequestExecutor和XMLHttpExecutor)
- HttpWebRequest与HttpWebResponse进行数据采集时的注意点
- HttpWebRequest.CookieContainer与HttpWebResponse.Cookies的区别和联系