"Get Web Response code 200" in C #

Source: Internet
Author: User
Tags response code try catch

Do a monitoring site of the C # project, to determine whether the site is working properly, the use is to get the HTTP header return status code.

For example, the status code of 200 is the site is normal, 403 is forbidden,404 is not found in this page.

The code is as follows:

The first is to use the library using System.Net;

public static bool GetState ()
{
String URL = "https://www.baidu.com/";
String StatusCode;
Try
{
HttpWebRequest req = (HttpWebRequest) webrequest.createdefault (new Uri (URL));
Req. method = "Head";//Set the request to the request header, so that you do not need to download the entire page
Req. Timeout = 10000;
HttpWebResponse res = (HttpWebResponse) req. GetResponse ();
StatusCode = Res. Statuscode.tostring ();
if (StatusCode = = "OK")
{
return true;
}
Else
{
return false;
}
}
catch (WebException ex)//use try Catch mode, if normal, return OK, abnormal return the corresponding error.
{
Console.WriteLine (ex);
return false;
}

}

"Get Web Response code 200" in C #

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.