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;
           }
      

posted @ 2014-11-20 19:50  除了影子  阅读(1837)  评论(0编辑  收藏  举报