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:
- Informational response (100 - 199)
- Successful response (200 - 299)
- Redirection message (300 - 399)
- Client error response (400 - 499)
- 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.