obizues
obizues

Reputation: 1483

Getting a HttpStatusCode of 0

Why am I getting a HttpStatusCode of 0 if I point the service my client is connecting to to a bad URL.

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);
        }

What is the proper way to handle this RestResponse?

Upvotes: 36

Views: 41100

Answers (10)

Tola Chhoeun
Tola Chhoeun

Reputation: 30

If you are using RestSharp, Method.Get request make sure you don't set Header "Content-Type": "application/json". Because RestSharp complained about Method.Get request cannot have any content in its request. Also, try set ThrowOnAnyError = true to see a more detailed error if there is any.

// Init Rest Client
var options = new RestClientOptions("https://SomeDomain.com")
{
    MaxTimeout = -1,
    RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true,
    ThrowOnAnyError = true
};
var client = new RestClient(options);

Upvotes: 0

SnowMan50
SnowMan50

Reputation: 7

The HttpStatusCode is set by the server/service that processes your HTTP requests. If you make a request that cannot be resolved to a valid server/service (bad URL, port, Security protocol, etc.), you simply cannot access the Http request processor that handles the request and there is no code that executes to set the HttpStatusCode - hence the default value of 0 for an integer type. If you are trying to catch this condition, make certain you catch the timeout exception on your Client Request (in the client code) and set the Httpcode to something that tells you what happened. I like HttpStatusCode 503 or 504 (Service Unavailable or Gateway Timeout) although the server can respond with those codes for other reasons so they are slightly ambiguous. Otherwise, just treat the Httpcode of 0 as a failure to reach the server/service for some network-related cause (again like no valid URL).

Upvotes: 0

brkngcn
brkngcn

Reputation: 11

If this line does not work

System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

then try this

System.Net.ServicePointManager.ServerCertificateValidationCallback = (senderX, certificate, chain, sslPolicyErrors) => { return true; };
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

this should work.

If I get StatusCode 0 issue during my post request then I use this two lines of code.

Upvotes: 1

user3063711
user3063711

Reputation: 49

You can try this on Windows Form:

System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

Thank you @KJ3

Upvotes: 4

Hakan Turan
Hakan Turan

Reputation: 1

I get intermittent response 0 from server behind firewall. Following code has solved my problem

client.ConfigureWebRequest((r) =>
        {
          r.ServicePoint.Expect100Continue = false;
          r.KeepAlive = true;
        });

Upvotes: 0

Val
Val

Reputation: 2090

In my case, using certificate authentication, the configured certificate was wrong. I hope this helps.

Upvotes: 0

lucidbrot
lucidbrot

Reputation: 6261

In my case, I simply specified an invalid hostname.

Upvotes: 0

Chris McCowan
Chris McCowan

Reputation: 487

I think the statuscode of 0 means there is no status code. It can also mean the error was in RestSharp, not the response. With a statuscode of 0, the request may not have even been sent.

Check for an for a .ErrorException / .ErrorMessage.

Upvotes: 14

KJ3
KJ3

Reputation: 5308

In my case it was not a firewall issue causing the StatusCode of 0. We were using a legacy app that was still using TLS 1.0 on a server that had blocked TLS 1.0 connections. Once we enabled TLS 1.2, we got the status codes we were expecting.

System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

Upvotes: 24

Neville
Neville

Reputation: 433

A response code of 0 generally means that the response was empty - i.e. not even headers were returned.

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.

Another thing to do is to change IRestResponse to RestResponse. Using IRestResponse offers no advantages in this scenario.

Upvotes: 24

Related Questions