Getting Http Status Code Number (200, 301, 404, etc.) from Httpwebrequest and Httpwebresponse

Getting Http Status code number (200, 301, 404, etc.) from HttpWebRequest and HttpWebResponse

Console.Write((int)response.StatusCode);

HttpStatusCode (the type of response.StatusCode) is an enumeration where the values of the members match the HTTP status codes, e.g.

public enum HttpStatusCode
{
...
Moved = 301,
OK = 200,
Redirect = 302,
...
}

How do I return the HTTP status code when I'm making a JSON post in C#?

It should be easy as

var client = new HttpClient();
var results = await client.GetAsync("https://stackoverflow.com");
Console.WriteLine(results.StatusCode);

Forcing C#'s HTTP Response to Return a Status Code Instead of a Description

With that said, it appears that StatusCode() doesn't actually return a "status code," and on successful requests, it only returns an OK instead of a 200.

No, it returns an HttpStatusCode enum value. If you call ToString on an enum value that has a name, it will return the name.

The simplest way of avoiding that is just to cast it to int:

headers.Add("Status Code: " + (int) ((HttpWebResponse) webResponse).StatusCode);

Or to make the rest of the block cleaner, cast the response once:

using (WebResponse webResponse = webRequest.GetResponse())
{
var httpResponse = (HttpWebResponse) webResponse;
headers.Add("URL: " + url);
headers.Add("Status Code: " + (int) httpResponse.StatusCode);
headers.Add("Status Description: " + httpResponse.StatusDescription + "\n");
}

(Note that when you're using string concatenation, ToString will be called implicitly if necessary - and it's never worth calling on something like StatusDescription which is already a string.)

c# - httpwebrequest status code returns 200 instead of 404

I actually don't get any status code when running this, I get a DNS error saying I cant lookup the domain.

I imagine you are exactly right about the ISP, they may be doing this via a DNS redirection given you dont get this error. You could solve this by using a DNS server other than the one your ISP provides, try googles 8.8.8.8, 8.8.4.4 (https://developers.google.com/speed/public-dns/)

This from their FAQs

How is Google Public DNS different from my ISP's DNS service or other
open DNS resolvers? How can I tell if it is better?

Open resolvers and
your ISP all offer DNS resolution services. We invite you to try
Google Public DNS as your primary or secondary DNS resolver along with
any other alternate DNS services. There are many things to consider
when identifying a DNS resolver that works for you, such as speed,
reliability, security, and validity of responses. Unlike Google Public
DNS, some ISPs and open resolvers block, filter, or redirect DNS
responses.

How does Google Public DNS handle non-existent domains?

If
you issue a query for a domain name that does not exist, Google Public
DNS always returns an NXDOMAIN record, as per the DNS protocol
standards. The browser should show this response as a DNS error. If,
instead, you receive any response other than an error message (for
example, you are redirected to another page), this could be the result
of the following: A client-side application such as a browser plug-in
is displaying an alternate page for a non-existent domain. Some ISPs
may intercept and replace all NXDOMAIN responses with responses that
lead to their own servers. If you are concerned that your ISP is
intercepting Google Public DNS requests or responses, you should
contact your ISP.

How can i retrieve the http response status code from an url?

You are not returning the response status code. You are returning the headers.

You should replace the return statement with this:

return ((int)response.StatusCode).ToString();


Related Topics



Leave a reply



Submit