C# 解决“请求被中止: 未能创建 SSL/TLS 安全通道”的问题

在请求中加入

System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; //加上这一句

以下为c# post 请求
 public static string HttpRequest(string url)
        {
            //string returnData = null;
            string ret = string.Empty;
            try
            {
                System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(url);
                webReq.Method = "POST";
                webReq.ContentType = "application/json";
                webReq.Headers.Add("Authorization", "bearer ********");
                Stream postData = webReq.GetRequestStream();
                postData.Close();
                HttpWebResponse webResp = (HttpWebResponse)webReq.GetResponse();
                StreamReader sr = new StreamReader(webResp.GetResponseStream(), Encoding.UTF8);
                ret = sr.ReadToEnd();
                
            }
            catch (Exception ex)
            {
                //LogManager.LogInstance.WriteLog("时间:" + DateTime.Now + "/n 请求出错原因" + ex.ToString());
            }
            return ret;
        }

  

原文地址:https://www.cnblogs.com/syeacfpl/p/14863535.html