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);
            }
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值