代码改变世界

HttpWebRequest和HttpWebRespone的常用方法总结

2011-06-29 19:35  calm_水手  阅读(551)  评论(0编辑  收藏  举报

一.访问 无验证,无登陆的URL

View Code
1  HttpWebRequest request = WebRequest.Create(resquestUrl) as HttpWebRequest;
2                 request.Method = "get";
3                 //request.Method = "post";
4                 HttpWebResponse response = request.GetResponse() as HttpWebResponse;
5                 StreamReader sr = new StreamReader(response.GetResponseStream());
6                 string returnContent = sr.ReadToEnd();
7                 sr.Close();
8                 response.Close();

二.访问有证书的URL

View Code
 1 //证书验证通过。
 2             ServicePointManager.ServerCertificateValidationCallback =
 3             new System.Net.Security.RemoteCertificateValidationCallback(true);
 4             // 与指定URL创建HTTP请求
 5             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
 6             request.Method = "post";
 7             //request.Method = "get";
 8             request.Accept = "text/html, application/xhtml+xml, */*";
 9             request.ContentType = "application/x-www-form-urlencoded";
10             //创建证书文件
11             X509Certificate objx509 = new X509Certificate(Application.StartupPath + "\\证书文件相对文件位置");
12             //添加相关证书到请求里
13             request.ClientCertificates.Add(objx509);
14             // 获取对应HTTP请求的响应
15             HttpWebResponse response = (HttpWebResponse)request.GetResponse();
16             // 获取响应流
17             Stream responseStream = response.GetResponseStream();
18             // 对接响应流(以"GBK"字符集)
19             StreamReader sReader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
20             string RequestResult = sReader.ReadToEnd();
21             sReader.Close();
22             response.Close();

三.访问 需要登录的URL

需要登录的URL从cookie里面去的session,在请求的时候,连带cookie信息一起发送过去即可

View Code
 1  // 这一句一定要写在创建连接的前面。使用回调的方法进行证书验证。
 2             ServicePointManager.ServerCertificateValidationCallback = 
 3             new System.Net.Security.RemoteCertificateValidationCallback(true);
 4             Encoding encoding = Encoding.Default;
 5             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
 6             //创建证书文件
 7             X509Certificate objx509 = new X509Certificate(Application.StartupPath + "\\证书文件相对文件位置");//request.Referer=URL;//验证网页来源
 8             //加载Cookie
 9             request.CookieContainer = new CookieContainer();
10             request.CookieContainer.Add(new Uri(),new Cookie("SessionId","SessionValue"));
11             //添加证书到请求里
12             request.ClientCertificates.Add(objx509);
13             request.Method = "post";
14             request.Accept = "text/html, application/xhtml+xml, */*";
15             request.ContentType = "application/x-www-form-urlencoded";
16             byte[] buffer = encoding.GetBytes(strPostdata);
17             request.ContentLength = buffer.Length;
18             request.GetRequestStream().Write(buffer, 0, buffer.Length);
19             HttpWebResponse response = (HttpWebResponse)request.GetResponse();
20             using (StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding(strEncoding)))
21             {
22                 return reader.ReadToEnd();
23             }

参考文章:http://www.cnblogs.com/sufei/archive/2011/05/22/2053642.html