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

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM