Welcome to deBUG.to Community where you can ask questions and receive answers from Microsoft MVPs and other experts in our community.
1 like 0 dislike
1k views
in Blog Post by 20 34 41
edited by

In this post, we will walk you through an example of requesting a website and receiving its status code.

HTTP Response Status Code Categories

First of all, let's discuss the difference between the five HTTP response status code categories:

  1. Informational response (100 - 199)
  2. Successful response (200 - 299)
  3. Redirection message (300 - 399)
  4. Client error response (400 - 499)
  5. Server error response (500 - 599)

How to Create a Web Request?

To create a web request you need to use the HttpWebRequest class that enables you to request a server using Http by using WebRequest.Create() method.Remember to add System.Net namespace so you can access theses classes.

using System.Net;

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(“https://debug.to”);

Then you need to get the response for that site request using the class HttpWebResponse and the method GetResponse()

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

Get HTTP status code in ASP.NET

Now you can get the status code for that response using the HttpStatusCode class 

HttpStatusCode statusCode = response.StatusCode;

Conclusion

In conclusion, we have explored the HTTP Response Status Code Categories and how to Get HTTP status code in ASP.NET.


If you don’t ask, the answer is always NO!
...