2016-01-14 26 views
5

一個HttpStatusCode爲什麼我收到的0 HttpStatusCode如果我點我的客戶端連接到一個壞的URL的服務。獲取的0

我statusCodeAsInt被顯示爲0。爲什麼不顯示爲404和正在處理的?

IRestResponse response = client.Execute(restReq); 
HttpStatusCode statusCode = response.StatusCode; 

var statusCodeAsInt = (int) statusCode; 

     if (statusCodeAsInt >= 500) 
     { 
      throw new InvalidOperationException("A server error occurred: " + response.ErrorMessage, response.ErrorException); 
     } 
     if (statusCodeAsInt >= 400) 
     { 
      throw new InvalidOperationException("Request could not be understood by the server: " + response.ErrorMessage, 
       response.ErrorException); 
     } 

什麼是正確的方式來處理這RestResponse?

回答

9

爲0的響應代碼通常意味着反應是空 - 即,不被退回甚至頭。

這當一個連接被接受,然後關閉擺好,也稱爲FIN連接通常發生。服務器指出它已經完成向您的廣播,但將繼續收聽新消息。 可能是防火牆問題。

另一件要做的事情是將IRestResponse更改爲RestResponse。在這種情況下使用IRestResponse沒有任何優勢。

+2

另外需要注意的是,如果請求命中一個有效的Web服務器時,纔會發生404錯誤。 Web服務器創建「404」狀態碼。 – BinaryEvolved

+1

謝謝,這很有幫助。 – obizues