首页 > Web开发 > 详细

System.Net.Http.HttpClient POST 未能创建 SSL/TLS 安全通道

时间:2020-06-22 09:30:41      阅读:124      评论:0      收藏:0      [点我收藏+]

技术分享图片

HttpRequestException: 发送请求时出错。

WebException: 请求被中止: 未能创建 SSL/TLS 安全通道。

 

方法调用前加以下代码行即可.

System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;

        static void Test()
        {
            System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;

            using (System.Net.Http.HttpClient httpClient = new System.Net.Http.HttpClient())
            {

                httpClient.MaxResponseContentBufferSize = 256000;
                httpClient.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36");
                httpClient.DefaultRequestHeaders.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");

                var param = new Dictionary<string, string> { { "", "<?xml version=‘1.0‘?><message><priority>0</priority></message>" } };

                var response = httpClient.PostAsync(@"https://www.baidu.com", new System.Net.Http.FormUrlEncodedContent(param)).Result;

                Console.WriteLine($"Response:{response.Content.ReadAsStringAsync().Result}");


            }

        }

技术分享图片

System.Net.Http.HttpClient POST 未能创建 SSL/TLS 安全通道

原文:https://www.cnblogs.com/honk/p/13174831.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!