C# 头检查总是 403?
我试图 ping 一个域列表,以获取它们是否存在的初始指示 - 如果它们不存在,我不会收到 ping 回来 - 这会抛出异常。不是问题,除了由于某种原因重定向也不会回复。
所以我试图获取异常 url 的 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);
}
}
}
提前致谢
编辑 Ping 错误如下:
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
im trying to ping a list of domains to get initial indications if they exist - if they dont exist i dont get a ping back - which throws and exception. not a problem, except for some reason redirects also dont ping back.
so im trying to get the http header responce of the exception url. but no matter what i get a 403 response. any ideas?
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);
}
}
}
thanks in advance
EDIT Ping error as follows:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的逻辑似乎是倒退的(无论如何我都读到了)。如果 ping 没有得到响应而抛出异常,那么向服务器发送
HEAD
请求通常是没有希望的,因为服务器可能不存在。除此之外,对于您想要完成的任务来说, ping 并不是真正的好选择。您可以在
Dns.GetHostAddresses
之间进行组合(如评论中建议的那样),然后尝试打开到端口 80 和/或 443 的 TCP 连接(适合您的网站)正在尝试使用TCPClient
类进行检查,以确定是否确实有服务器在侦听发现的 IP。几乎没有其他方法可以真正验证是否确实有服务器正在侦听您要检查的 IP。但它不会检查 URL 是否有效,因此在证明服务器存在后可能会添加一个HEAD
,这是一个很好的补充。Your logic appears to be backwards (as I read it anyway). If the ping throws an exception if it doesn't get a response then sending a
HEAD
request to the server is usually hopeless since the server probably doesn't exist.Beyond that ping is not really a good option for what you're trying to accomplish. You could do a combination between a
Dns.GetHostAddresses
(as was suggested in the comments) and just try to open a TCP connection to port 80 and/or 443 (which ever is appropriate for the sites you're trying to check) with theTCPClient
class to determine if there actually is a server listening on the IP discovered. There is little else that will actually verify that there is actually a server listening on the IP you're trying to check. It doesn't check if the URL is valid though so maybe aHEAD
after proving the server is there would be a good addition.