标题检查C#始终为403?
问题描述:
即时通讯尝试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如果不是获得响应,则抛出异常,则向服务器发送HEAD
请求通常是无望的,因为服务器可能不存在。
除此之外,对于您要完成的任务来说,ping并不是一个好选择。您可以在Dns.GetHostAddresses
(如评论中所建议的)之间进行组合,然后尝试使用TCPClient
类打开到端口80和/或443的TCP连接(这适用于您尝试检查的站点)以确定是否实际上有服务器正在侦听所发现的IP。除此之外,还没有其他的东西会确实证实实际上有一台服务器正在监听您正在尝试检查的IP。它不会检查URL是否有效,但验证服务器是否会有一个很好的补充后,可能是HEAD
。
由于许多防火墙将阻止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