2016-01-14 24 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