Set the SecurityProtocol (Ssl3 or TLS) on the .net HttpWebRequest per request

前端 未结 9 1252
执笔经年
执笔经年 2020-11-28 07:10

My application (.net 3.5 sp1) uses the HttpWebRequest to communicate with different endpoints, sometimes its over HTTPS where each hosting server may have a different securi

9条回答
  •  星月不相逢
    2020-11-28 07:40

    With Net 4.6 there is HttpClient and WinHttpHandler nuget package available for windows (from microsoft) to set SslProtocols parameters. With Net core you can use HttpClientHandler class for the same.

    using (var hc = new HttpClient(new WinHttpHandler() // should have it as a static member
    {
        AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip,
        SslProtocols = SslProtocols.Tls | 
                       SslProtocols.Tls11 | 
                       SslProtocols.Tls12
    }))
    {
        var r = hc.SendAsync(new HttpRequestMessage(HttpMethod.Get, "https://..."));
        r.Wait();
        Console.WriteLine(r.Result.StatusCode);
    } // using
    

提交回复
热议问题