![](/img/trans.png)
[英]HttpWebRequest.GetResponse() , does every StatusCode besides 200 throws exception?
[英]Does HttpWebRequest.GetResponse() always throw a WebException if anything other than 200 is returned by the server?
不,WebException只會在某些情況下拋出,主要是因為在處理請求時超時和錯誤。
這是HttpWebRequest.GetResponse()的文檔:
https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.getresponse(v=vs.110).aspx
請注意,引發WebException的三個主要原因:
Abort was previously called.
-or-
The time-out period for the request expired.
-or-
An error occurred while processing the request.
倒數第二個是最常見的,您會經常從中看到400個。 在文檔中,他們有很好的建議,您可以用來嘗試診斷問題的確切原因:
如果引發WebException,請使用異常的Response和Status屬性來確定服務器的響應。
如果您使用的是可以放置斷點的IDE(Visual Studio),請檢查響應和狀態。 如果不是,請使用Console.Writeline()打印響應或狀態代碼,然后從那里開始調查。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.