c# https get請求 基礎連線已關閉,接受時發生錯誤 解決方法
阿新 • • 發佈:2019-01-02
用HttpWebRequest模擬傳送https請求,提示錯誤: 基礎連線已關閉,接受時發生錯誤
在本地測試ok,開發環境為64位win7 vs2013 釋出到windows server 2008 x86系統上執行一直報錯。。
下載2008 r2 x64執行發現數據正常。。
public static string GetUrl(string url) { if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) { System.Console.WriteLine("https connections....."); ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); // 這裡設定了協議型別。 //ServicePointManager.SecurityProtocol =SecurityProtocolType.Tls12;// (SecurityProtocolType)3072;// SecurityProtocolType.Tls1.2; ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; //(SecurityProtocolType)768 | (SecurityProtocolType)3072 ServicePointManager.CheckCertificateRevocationList = true; ServicePointManager.DefaultConnectionLimit = 100; ServicePointManager.Expect100Continue = false; } HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; //增加下面兩個屬性即可 request.KeepAlive = false; request.ProtocolVersion = HttpVersion.Version10; request.Method = "GET"; //使用get方式傳送資料 request.ContentType = "application/x-www-form-urlencoded"; request.Referer = ""; request.AllowAutoRedirect = true; request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727)"; request.Accept = "*/*"; //獲取網頁響應結果 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); //Thread.Sleep(2000); Stream stream = response.GetResponseStream(); //client.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); string result = string.Empty; using (StreamReader sr = new StreamReader(stream)) { result = sr.ReadToEnd(); } if (request != null) { request.Abort(); } if(response !=null) { response.Close(); } return result; }