前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >c# HttpWebRequest https的一些处理

c# HttpWebRequest https的一些处理

作者头像
冰封一夏
发布2019-09-11 15:22:30
2.9K0
发布2019-09-11 15:22:30
举报

先看下请求方法

 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协议

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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-06-25 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档