2013-07-15 2 views
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 상태가 온라인 또는 오프라인 여부를 확인하기 위해 다른 예를 들어 오프라인 상태 나는 url를 입력, winforms에서 버튼을 클릭하면 내가 온라인이라고 말하면 입력됩니다.가 어떻게

답변

0

적극적으로 인터넷에 핑을 걸지 않으려면 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. 
} 
+0

당신은 내게 예제를 줄 수 있습니까? – user2132564