2012-01-16 47 views
1

即時通訊嘗試ping一個域名列表,以獲得最初的跡象,如果他們存在 - 如果他們不存在,我不會得到一個回ping - 拋出和異常。不是一個問題,除了某些原因重定向也不ping通。標題檢查C#始終爲403?

所以即時通訊嘗試獲取異常網址的http標頭響應。但不管我得到403迴應。有任何想法嗎?

private void hunt_Click(object sender, EventArgs e) 
    { 
     listBox1.Items.Clear(); 
     string hostAddress = txtKeyword.Text; 
     string combined; 

     string[] strArray = new string[] { ".com", ".net", ".org", ".ca", ".gov" }; 

     foreach (string str in strArray) 
     { 

      combined = hostAddress + str; 


      string result = string.Empty; 
      try 
      { 
       Ping ping = new Ping(); 
       int timeout = 1500; 
       PingReply pingreply = ping.Send(combined, timeout); 


       if (pingreply != null && pingreply.Status.ToString() != "TimedOut") 
       { 
        result = "Address: " + pingreply.Address + "\r" 
         + "Roundtrip Time: " + pingreply.RoundtripTime + "\r" 
         + "TTL (Time To Live): " + pingreply.Options.Ttl + "\r" 
         + "Buffer Size: " + pingreply.Buffer.Length + "\r"; 

        listBox1.Items.Add(combined + " " + result); 
       } 
       else 
       { 

        listBox1.Items.Add(combined + " not found"); 
       } 

      } 


      catch (Exception pingError) 
      { 
       HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www."+combined); 
       request.Method = "HEAD"; 
       HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
       HttpStatusCode status = response.StatusCode; 
       listBox1.Items.Add(status); 
      } 

     } 
    } 

在此先感謝

編輯坪錯誤如下:

System.Net.NetworkInformation.PingException: An exception occurred during a Ping  request. ---> System.Net.Sockets.SocketException: No such host is known 
    at System.Net.Dns.GetAddrInfo(String name) 
    at System.Net.Dns.InternalGetHostByName(String hostName, Boolean includeIPv6) 
    at System.Net.Dns.GetHostAddresses(String hostNameOrAddress) 
    at System.Net.NetworkInformation.Ping.Send(String hostNameOrAddress, Int32 timeout,  Byte[] buffer, PingOptions options) 
    --- End of inner exception stack trace --- 
    at System.Net.NetworkInformation.Ping.Send(String hostNameOrAddress, Int32 timeout,  Byte[] buffer, PingOptions options) 
    at System.Net.NetworkInformation.Ping.Send(String hostNameOrAddress, Int32 timeout) 
    at DomainHunter.Form1.hunt_Click(Object sender, EventArgs e) in 
+4

由於許多防火牆將阻止ping請求,因此ping將不可靠。如果你只是想檢查域是否存在,你可能會更好使用(例如)['Dns.GetHostAddresses'](http://msdn.microsoft.com/en-us/library/system.net。 dns.gethostaddresses.aspx)。 – porges 2012-01-16 01:16:05

+0

pingError.Message或pingError.StackTrace能爲您提供任何有用的信息嗎? – Brissles 2012-01-16 01:17:13

+0

檢查這個問題:http://stackoverflow.com/questions/2646755/c-net-find-out-whether-a-server-exists-query-dns-for-svc-records – 2012-01-16 01:18:53

回答

1

你的邏輯似乎是向後(因爲我讀也無妨)。如果ping如果不是獲得響應,則拋出異常,則向服務器發送HEAD請求通常是無望的,因爲服務器可能不存在。

除此之外,對於您要完成的任務來說,ping並不是一個好選擇。您可以在Dns.GetHostAddresses(如評論中所建議的)之間進行組合,然後嘗試使用TCPClient類打開到端口80和/或443的TCP連接(這適用於您嘗試檢查的站點)以確定是否實際上有服務器正在偵聽所發現的IP。除此之外,還沒有其他的東西會確實證實實際上有一臺服務器正在監聽您正在嘗試檢查的IP。它不會檢查URL是否有效,但驗證服務器是否會有一個很好的補充後,可能是HEAD