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

转载于:https://www.cnblogs.com/yixingmeng/p/4111372.html

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:编程工作室 设计师:CSDN官方博客 返回首页
评论
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值