当你的才华不能撑起你的野心时,就是你该选择学习的时候了!

HttpClient HttpClientHandler WebRequestHandler Explained 处理https请求

Framework 4.8的 HttpClientHandler 有 ServerCertificateCustomValidationCallback 所以处理https请求做如下操作:

var handler = new HttpClientHandler();

            var baseUri = new Uri(url);
            if (baseUri.Scheme.ToLower() == "https")
            {
                handler.UseDefaultCredentials = true;
                handler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; };
            }
            var httpClient = new HttpClient(handler);
            var response = await httpClient.PostAsync(url, new StringContent(postJson, Encoding.UTF8, "text/json"));
            string responseJson = await response.Content.ReadAsStringAsync();

 

Framework 4.6.1的 HttpClientHandler 没有 ServerCertificateCustomValidationCallback 所以处理https请求做如下操作:

 

var handler = new WebRequestHandler();
handler.ServerCertificateValidationCallback = delegate { return true; };

 

// Create WebRequestHandler and set UseDefaultCredentials and AllowPipelining (for HTTP) properties
WebRequestHandler webRequestHandler = new WebRequestHandler();
webRequestHandler.UseDefaultCredentials = true;
webRequestHandler.AllowPipelining = true;

// Create an HttpClient using the WebRequestHandler
HttpClient client = new HttpClient(webRequestHandler);

// Create an HttpClient and add message handlers for the client
HttpClient client = HttpClientFactory.Create(
    clientHandler,
    new SampleHandler("Client A", 2),
    new SampleHandler("Client B", 4),
    new SampleHandler("Client C", 6));

 

WebRequestHandler的代码运行时会报错:The remote certificate is invalid according to the validation procedure.

解决“The remote certificate is invalid according to the validation procedure”问题 - dudu - 博客园 (cnblogs.com)

 

参考:

解决“The remote certificate is invalid according to the validation procedure”问题 - dudu - 博客园 (cnblogs.com)

HttpClient, HttpClientHandler, and WebRequestHandler Explained_weixin_30767835的博客-CSDN博客

.net core中Grpc使用报错:The remote certificate is invalid according to the validation procedure. - chester·chen - 博客园 (cnblogs.com)

 

扩展:

4.6 .NET Framework 包括一项新的安全功能,用于阻止连接的不安全密码和哈希算法。 默认情况下,通过 Api (例如 HttpClient、HttpWebRequest、FtpWebRequest、SmtpClient、4.6 SslStream等)使用 TLS/SSL 的应用程序将获得更安全的行为。

如需关闭该安全行为,只需要如此即可~请勿设置System.Net.ServicePointManager.SecurityProtocol!

        private const string DisableCachingName = @"TestSwitch.LocalAppContext.DisableCaching";
        private const string DontEnableSchUseStrongCryptoName = @"Switch.System.Net.DontEnableSchUseStrongCrypto";

        static void Main(string[] args)
        {
            AppContext.SetSwitch(DisableCachingName, true);
            AppContext.SetSwitch(DontEnableSchUseStrongCryptoName, true);
            var handler = new HttpClientHandler();
            using (var http2 = new HttpClient(handler))
            {
                var r = http2.GetAsync("https://*****:8888/");
                r.Wait();
                Console.WriteLine(r.Result);
            }
        }

 

posted @ 2023-09-21 16:28  hofmann  阅读(132)  评论(0)    收藏  举报