解析IP失败

发布于 2024-07-09 02:27:33 字数 430 浏览 3 评论 0原文

尽我所能,我无法将地址解析为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

紫罗兰の梦幻 2024-07-16 02:27:33

你的代码没有任何问题。 鉴于您可以从网络浏览器访问 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.

后来的我们 2024-07-16 02:27:33

您需要设置代理:

这是一个应该为以下所有调用设置代理的代码片段:

    protected void SetupProxy(string proxyUrl, string proxyLogin, string proxyPassword, string[] proxyBypass)
    {
        WebProxy proxy = new WebProxy(proxyUrl);
        proxy.Credentials = new NetworkCredential(proxyLogin, proxyPassword);
        proxy.BypassList = proxyBypass;
        proxy.BypassProxyOnLocal = true;
        WebRequest.DefaultWebProxy = proxy;
    }

You need to set up the proxy:

here's a snippet that should set it up for all the following calls:

    protected void SetupProxy(string proxyUrl, string proxyLogin, string proxyPassword, string[] proxyBypass)
    {
        WebProxy proxy = new WebProxy(proxyUrl);
        proxy.Credentials = new NetworkCredential(proxyLogin, proxyPassword);
        proxy.BypassList = proxyBypass;
        proxy.BypassProxyOnLocal = true;
        WebRequest.DefaultWebProxy = proxy;
    }
两人的回忆 2024-07-16 02:27:33

不要尝试通过浏览器,而是尝试从命令行 ping www.google.com(当然也可以是其他主机)。

ping 本身可能不起作用,但它应该首先显示 IP 地址解析。 如果您收到如下错误消息:

    Ping request could not find host www.google.com.
    Please check the name and try again.

那么很可能是代理服务器在您浏览时正在为您执行 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:

    Ping request could not find host www.google.com.
    Please check the name and try again.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文