簡體   English   中英

System.Net.HttpWebRequest.GetResponse()中的錯誤“ResponceSystem.Net.WebException:請求已中止:無法創建SSL / TLS安全通道”

[英]Error in System.Net.HttpWebRequest.GetResponse() “ResponceSystem.Net.WebException: The request was aborted: Could not create SSL/TLS secure channel”

Dotnet應用程序使用dotnet 4.5構建。 在少數系統上,我們在連接到最近在SSL中進行了少量更改的Web服務器時收到此錯誤。

Web服務器僅限TLS 1.2 https://drive.google.com/open?id=1Z0S-MWugDZdQrIy3BnquooB0ZX7veIVT

客戶端代碼。

WebRequest webRequest = WebRequest.Create(strUrl);
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Method = "Get";
if (strUrl.StartsWith("HTTPS", StringComparison.OrdinalIgnoreCase)) ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls | SecurityProtocolType.Ssl3;
using (WebResponse webResponse = webRequest.GetResponse())
{
    if (webResponse == null) return blnResult;
    using (StreamReader sr = new StreamReader(webResponse.GetResponseStream()))
    {
       strSuccess = sr.ReadToEnd().Trim();
       blnResult = true;
    }
}

只要我用框架4.6.2編譯它就可以了。

為什么它使用相同的4.5編譯EXE在少數系統上工作,在少數系統上它需要用4.6.2編譯?

WebClient類適用於4.5版本的dotnet框架。 只有WebRequest不起作用。

在初始化WebRequest對象之前,需要先設置安全協議。

if (strUrl.StartsWith("HTTPS", StringComparison.OrdinalIgnoreCase)) ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls | SecurityProtocolType.Ssl3;

WebRequest webRequest = WebRequest.Create(strUrl);
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Method = "Get";

using (WebResponse webResponse = webRequest.GetResponse())
{
    if (webResponse == null) return blnResult;
    using (StreamReader sr = new StreamReader(webResponse.GetResponseStream()))
    {
       strSuccess = sr.ReadToEnd().Trim();
       blnResult = true;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM