C#判断网站是否可以正常访问
String PageUrl = "url";
try
{
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(PageUrl);
myHttpWebRequest.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
if (myHttpWebResponse.StatusCode == HttpStatusCode.OK)
{
LabelMessage.Text="Response Status Code is OK and StatusDescription is: "+myHttpWebResponse.StatusDescription;
}
myHttpWebResponse.Close();
}
catch(WebException ee)
{
LabelMessage.Text = "WebException Raised. The following error occured : " + ee.Status;
}
catch(Exception ee)
{
LabelMessage.Text = "The following Exception was raised : " + ee.Message;
}