c# HttpWebRequest https的一些處理

來源:互聯網
上載者:User

標籤: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請求的時候需要確定加密協議是哪個,這個可以通過Firefox查看到,如

2、只有Framework4.5及以上才支援1.1和1.2協議

如果仍有什麼不明白的地方請留言吧

c# HttpWebRequest https的一些處理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.