码迷,mamicode.com
首页 > Windows程序 > 详细

c# HttpWebRequest https的一些处理

时间:2017-06-25 17:46:49      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:chain   erer   ict   get   cer   err   default   time   apple   

先看下请求方法

 1   public string Get_Request(
 2             string strUrl,
 3             CookieContainer _cookie = null,
 4             string strHost = "",
 5             string strRefer = "",
 6             string strOrigin = "",
 7             bool blnHttps = false,
 8             Dictionary<string, string> lstHeads = null,
 9             bool blnKeepAlive=false,
10             string strEncoding = "utf-8",
11             string strContentType = "",
12             string strCertFile="",
13             string strAccept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
14             string strUserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36",
15             bool blnAllowAutoRedirect = true,
16             int intTimeout = 1000 * 30)
17         {
18             HttpWebRequest request;
19             HttpWebResponse response;
20             request = (HttpWebRequest)WebRequest.Create(strUrl);
21             if (blnHttps)
22             {
23                 ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);  
24                 request.ProtocolVersion = HttpVersion.Version10;
25 
26                 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
27 
28             }           
29             request.KeepAlive = blnKeepAlive;
30             request.Accept = strAccept;
31             request.Timeout = intTimeout;
32             request.Method = "GET";
33             request.Credentials = CredentialCache.DefaultCredentials;
34             request.UserAgent = strUserAgent;
35             request.AllowAutoRedirect = blnAllowAutoRedirect;
36             request.Proxy = null;
37             if (!string.IsNullOrEmpty(strContentType))
38             {
39                 request.ContentType = strContentType;
40             }
41             if (_cookie != null)
42             {
43                 request.CookieContainer = _cookie;
44             }
45             if (!string.IsNullOrEmpty(strHost))
46             {
47                 request.Host = strHost;
48             }
49             if (!string.IsNullOrEmpty(strRefer))
50             {
51                 request.Referer = strRefer;
52             }
53             if (!string.IsNullOrEmpty(strOrigin))
54             {
55                 request.Headers.Add("Origin", strOrigin);
56             }
57             if (lstHeads != null && lstHeads.Count > 0)
58             {
59                 foreach (var item in lstHeads)
60                 {
61                     request.Headers.Add(item.Key, item.Value);
62                 }
63             }
64             response = (HttpWebResponse)request.GetResponse();
65             var sr = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(strEncoding));
66             string strResult = sr.ReadToEnd();
67             sr.Close();
68             request.Abort();
69             response.Close();
70             return strResult;
71 
72         }
73         private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
74         {
75             return true; //总是接受     
76         }  
if (blnHttps)内的代码就是针对https所做的处理

需要注意的是

1、当使用https请求的时候需要确定加密协议是哪个,这个可以通过火狐查看到,如下图

技术分享

2、只有Framework4.5及以上才支持1.1和1.2协议

如果仍有什么不明白的地方请留言吧

c# HttpWebRequest https的一些处理

标签:chain   erer   ict   get   cer   err   default   time   apple   

原文地址:http://www.cnblogs.com/bfyx/p/7077173.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!