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 #