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 物件管理的 ServicePoint 物件所使用的安全性通訊協定。

根據官方說明
在.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

Getting a HttpStatusCode of 0

Http接口RestSharp中StatusCode返回0, 但postman获取应答正常?

HTTP 狀態碼

TLS 1.2 and .NET Support: How to Avoid Connection Errors

[faq]解決C#呼叫有ssl憑證問題的網站出現遠端憑證是無效的錯誤問題


.Net 4.0 要強迫使用 TLS 1.2 抓資料


[C#] 基礎連接已關閉: 傳送時發生未預期的錯誤

C# 連線 HTTPS 網站發生驗證失敗導致基礎連接已關閉


Failure on HttpWebrequest with inner exception Authentication failed because the remote party has closed the transport stream



留言

這個網誌中的熱門文章

經得起原始碼資安弱點掃描的程式設計習慣培養(五)_Missing HSTS Header

經得起原始碼資安弱點掃描的程式設計習慣培養(三)_7.Cross Site Scripting(XSS)_Stored XSS_Reflected XSS All Clients

(2021年度)駕訓學科筆試準備題庫歸納分析_法規是非題