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?
HTTP StatusCode=0 is associated with incomplete capture of a hit or page and often with a labeling of the hit as: request canceled ("ReqCancelled=Client" "ReqCancelled=Server" or "ReqCancelled=True").
Status code 0 means the requested url is not reachable.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With