zoukankan      html  css  js  c++  java
  • System.Net.Http.HttpClient POST 未能创建 SSL/TLS 安全通道

    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}");
    
    
                }
    
            }

  • 相关阅读:
    SQL所有者更改问题
    人生路上对我影响最大的三位老师
    自我介绍
    Ural_1146. Maximum Sum (DP)
    Ural_1654. Cipher Message(栈)
    Ural_1333. Genie Bomber 2
    POJ_2112 Optimal Milking(网络流)
    Ural_1031.Railway Ticket (DP)
    Ural_1030. Titanic
    Ural_1207. Median on the Plane(计算几何)
  • 原文地址:https://www.cnblogs.com/honk/p/13174831.html
Copyright © 2011-2022 走看看