0
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,否则为离线状态,例如当我输入url时,在 winforms 中单击按钮,如果我不输入,它会显示离线。

4

1 回答 1

0

如果您不想主动 ping 互联网,可以使用TryCreate或仅测试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.
}
于 2013-07-15T04:48:45.953 回答