解析IP失败
尽我所能,我无法将地址解析为 IP。 代码片段如下所示。 即使我可以使用浏览器访问 google(DNS 服务器几乎肯定可以正常工作),我仍然收到“没有已知的主机”异常。 然而我在公司的防火墙后面。
try
{
foreach (IPAddress address in Dns.GetHostAddresses("www.google.com"))
{
Console.WriteLine(address.ToString());
}
}
catch (SocketException e)
{
Console.WriteLine("Source : " + e.Source); // System
Console.WriteLine("Message : " + e.Message); // No such host is known
}
Try as I might, I'm unable to resolve an address to IP. The code snippet is shown below. I keep getting the No such host is known exception, even though I could access google with my browser (The DNS server is almost certainly working). I'm however behind company's firewall.
try
{
foreach (IPAddress address in Dns.GetHostAddresses("www.google.com"))
{
Console.WriteLine(address.ToString());
}
}
catch (SocketException e)
{
Console.WriteLine("Source : " + e.Source); // System
Console.WriteLine("Message : " + e.Message); // No such host is known
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的代码没有任何问题。 鉴于您可以从网络浏览器访问 www.google.com,下一个最可能的问题是网络浏览器正在使用代理服务器。 Web 浏览器实际上是通过允许通过防火墙的代理服务器访问 www.google.com。 您编写的简单应用程序不允许通过防火墙,因此会导致异常。
您可以通过查看 Internet Explorer 中的代理设置来验证这一点。
工具-> 选项-> 连接-> Lan 设置
将会有一组代理服务器设置。 如果存在价值,那么这几乎肯定是您的问题。
There is nothing wrong with your code. Given that you can access www.google.com from a web browser the next most likely problem is that the web browser is using a proxy server. The web browser is actually accessing www.google.com through the proxy server which is allowed through the firewall. The simple application you wrote is not allowed through the firewall and is resulting in an exception.
You can verify this by looking at the proxy settings in Internet Explorer.
Tools -> Options -> Connections -> Lan Settings
There will be a proxy server group of settings. If there is a value present, this is almost certainly your problem.
您需要设置代理:
这是一个应该为以下所有调用设置代理的代码片段:
You need to set up the proxy:
here's a snippet that should set it up for all the following calls:
不要尝试通过浏览器,而是尝试从命令行 ping www.google.com(当然也可以是其他主机)。
ping 本身可能不起作用,但它应该首先显示 IP 地址解析。 如果您收到如下错误消息:
那么很可能是代理服务器在您浏览时正在为您执行 DNS 查找,而您的 DNS 服务器要么无法工作,要么您计算机的网络设置不正确。
Rather than try through a browser, try pinging www.google.com (or some other host, of course) from the command line.
The ping itself may well not work, but it should show the IP address resolution first. If you get an error message like this:
then it's likely that the proxy server is doing the DNS lookup for you when you're browsing, and your DNS server is either not working or your machine's network settings are incorrect.