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
由於許多防火牆將阻止ping請求,因此ping將不可靠。如果你只是想檢查域是否存在,你可能會更好使用(例如)['Dns.GetHostAddresses'](http://msdn.microsoft.com/en-us/library/system.net。 dns.gethostaddresses.aspx)。 – porges 2012-01-16 01:16:05
pingError.Message或pingError.StackTrace能爲您提供任何有用的信息嗎? – Brissles 2012-01-16 01:17:13
檢查這個問題:http://stackoverflow.com/questions/2646755/c-net-find-out-whether-a-server-exists-query-dns-for-svc-records – 2012-01-16 01:18:53