获取请求错误"Could not create SSL/TLS secure channel"



我尝试从ConsoleApp:请求网站页面

static void Main(string[] args)
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var handler = new HttpClientHandler
{
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
,
AllowAutoRedirect = false
};
using (var client = new HttpClient(handler))
{
var response = client.GetAsync("https://lk.fssprus.ru/ds_cabinet/action/login");
var httpResponseMessage = response.Result;
var content = httpResponseMessage.Content.ReadAsStringAsync();
Console.WriteLine(content.Result);
}
Console.ReadKey();
}

并得到这个错误:

System.AggregateException HResult=0x80131500消息=发生一个或多个错误。Source=mscorlib StackTrace:位于System.Threading.Tasks.Task.SthrowIfExceptional(布尔includeTaskCanceledExceptions(位于的System.Threading.Tasks.Task1.GetResultCore(Boolean waitCompletionNotification) at System.Threading.Tasks.Task1.get_Result((中的ConsoleApp8.Program.Main(String[]args(C: \Projects\FsspConnectTest\ConsoleApp8\Program.cs:line 26

此异常最初是在此调用堆栈中引发的:System.Net.HttpWebRequest.EndGetResponse(System.IAsyncResult(System.Net.Http.HttpClientHandler.GetResponseCallback(System.IAsyncResult(

内部异常1:HttpRequestException:在发送请求。

内部异常2:WebException:请求已中止:无法创建SSL/TLS安全通道。

我无法解决这个问题

更新:我在Windows 10上进行了测试,它非常完美,但在我的Windows 8.1上不起作用。Windows 8.1需要安装什么?

有两件事-

  1. ">在.NET核心中,ServicePointManager只影响HttpWebRequest。它不会影响HttpClient。在.NET Framework中,内置的HttpClient是在HttpWebRequest之上构建的,因此ServicePointManager设置将应用于它

    因此,在HttpClientHandler中设置安全协议,并将其传递给HttpClient构造函数。

  2. 验证是否在计算机上禁用了所需的TLS版本。既然你说它在一个地方工作得很好,但在另一个地方失败了,而TLS1.2是Windows 8.1上启用的默认协议,那么可能就是这样。以下是您的操作方法-传输层安全性(TLS(注册表设置。

相关内容

最新更新