【问题标题】:How to check whether url status of a website is online or offline when i click the button单击按钮时如何检查网站的url状态是在线还是离线
【发布时间】:2013-07-12 21:11:51
【问题描述】:
private void button1_Click_1(object sender, EventArgs e)    
    try
        {
            ConnectionId.HostName = "host2.dnswind.com";
            Uri sUrl =new Uri("http://www.chaton.dilipdotnet.com");
            MyWebReq = (System.Net.HttpWebRequest)WebRequest.Create(sUrl);
            MyWebRes = (System.Net.HttpWebResponse)MyWebReq.GetResponse();
            sStream = MyWebRes.GetResponseStream();
            string reader = new StreamReader(sStream).ReadToEnd();
            //if (MyWebRes.StatusCode == HttpStatusCode.OK)
            {
               WebClient client = new WebClient();
               Stream oStream = MyWebRes.GetResponseStream();
               StreamReader oReader = new StreamReader(oStream);
               string sResult = oReader.ReadToEnd();

               if (sUrl.AbsoluteUri.ToString()=true)//here i want to check  
               {
                    MessageBox.Show("connection online");
               }
               else
               {
                   MessageBox.Show("Connection offline");
               }
            }

            }
            catch (Exception ex)
            {
                 //Url not valid
                 MessageBox.Show("Sorry you have typed wrong", ex.Message);

            }

         }

我想检查用户是否输入了它在线的网址,否则为离线状态 例如,当我输入url 时,在winforms 中单击该按钮,如果我不输入,它会显示在线。

【问题讨论】:

    标签: c# asp.net winforms url web


    【解决方案1】:

    您可以使用TryCreate 或者如果您不想主动ping 互联网,只需测试IsWellFormedUriString。格式良好的 URI 意味着符合某些 RFC。在文档中阅读更多内容。

    string strUri = "http://www.microsoft./en-us/default.aspx";
    Uri objUri = null;
    // Test the URI
    if (Uri.TryCreate(strUri, UriKind.RelativeOrAbsolute, out objUri))
    {
        // Uri was good.
    }
    else
    {
        // Uri was not good.
    }
    

    【讨论】:

    • 先生你能举个例子吗
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-17
    • 1970-01-01
    • 1970-01-01
    • 2012-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多