HttpClient GetAsync(url)抛出“请求已中止。无法创建SSL / TLS安全通道。”

时间:2017-10-25 12:26:13

标签: c# ssl httpclient

我尝试过以下C#代码,但在调用GetAsync()时会出现异常。使用具有相同请求的Firefox我能够得到回复。任何想法如何使代码在C#中工作?服务器很旧,不受我的控制。

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
var credentials = new NetworkCredential("user", "password");
var handler = new HttpClientHandler { Credentials = credentials };
var client = new HttpClient(handler);
var url = $"https://ipadress/query";
HttpResponseMessage httpResponseMessage = await client.GetAsync(url);

if (httpResponseMessage.IsSuccessStatusCode)
{
    //
}

0 个答案:

没有答案