获得HttpStatusCode为0 [英] Getting a HttpStatusCode of 0
问题描述
如果我将客户端连接的服务指向错误的URL,为什么我的HttpStatusCode为0。
Why am I getting a HttpStatusCode of 0 if I point the service my client is connecting to to a bad URL.
我的statusCodeAsInt显示为0。为什么它没有显示为404并被处理?
My statusCodeAsInt is showing up as a 0. Why is it not showing up as a 404 and being handled?
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的正确方法是什么?
What is the proper way to handle this RestResponse?
推荐答案
响应代码0通常表示响应为空 - 即不返回标题。
A response code of 0 generally means that the response was empty - i.e. not even headers were returned.
这通常在接受连接时发生,然后正常关闭,也称为FIN连接。这是服务器声明已完成广播的地方,但会继续收听新消息。 可能是防火墙问题。
This usually happens when a connection is accepted, and then closed gracefully, also known as a FIN connection. Which is where the server states that it has finished broadcasting to you, but will continue to listen for new messages. Could be a firewall issue.
另一件事是将 IRestResponse
更改为 RestResponse
。在这种情况下,使用IRestResponse没有任何优势。
Another thing to do is to change IRestResponse
to RestResponse
. Using IRestResponse offers no advantages in this scenario.
这篇关于获得HttpStatusCode为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!