RestSharp 回傳HttpStatusCode為0的異常_基礎連接已關閉: 應該保持運作的連接卻被伺服器關閉_ServicePointManager 類別

近期協助歐洲專利網站api資料存取過程 遇到一個離奇問題 status code回傳了0這個詭異的數字 一般而言,HTTP Status code只會存在下列範圍 資訊回應 (Informational responses, 100–199) 成功回應 (Successful responses, 200–299) 重定向 (Redirects, 300–399) 用戶端錯誤 (Client errors, 400–499) 伺服器端錯誤 (Server errors, 500–599) 在經過大量爬文後發現跟以前提過的 停用 TLS 1.0 有點相關 微軟、蘋果、Google及Mozilla四大瀏覽器業者將在2020年終止支援TLS 1.0、1.1 TLS 1.2 (Transport Layer Security version 1.2)理論上應該目前都改用此標準了 ServicePointManager 類別 當中的 SecurityProtocol屬性 能讓我們取得或設定由 ServicePointManager 物件管理的 ServicePoint 物件所使用的安全性通訊協定。 根據官方說明 .net4.6之後理應預設都已經自動啟用TLS1.2 在.net4.5以下時期TLS1.2根本沒有被支援 但已經使用是.net4.6了.... 看來可能還是要透過程式做相關設定 1.在call api的共用呼叫地方 調整 ServicePointManager.SecurityProtocol 擴大 SSL/TLS 版本範圍 或強制TLS1.2 1 ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; 2.把IRestResponse改為RestResponse 3.調整RestClient 重新try 一陣子較比較穩定 Ref: RestSharp returns Status '0' while calling post method https://stackoverflow.com/questi...