標籤: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的一些處理