5

I'm trying to resolve a host name to its' corresponding IP.

My environment is a mac in a corporate network behind a proxy server, which is configured with a .pac file via the system preferences (automatic proxy configuration). So far everything is working fine and I can access resources inside and outside my corporate network.

Resolving hosts within my network works perfectly fine: InetAddress.getByName("host.local");

But when I use external host names, I get a UnknownHostException: InetAddress.getByName("google.com");

produces

Exception in thread "main" java.net.UnknownHostException: google.com
    at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
    at java.net.InetAddress$1.lookupAllHostAddr(InetAddress.java:850)
    at java.net.InetAddress.getAddressFromNameService(InetAddress.java:1201)
    at java.net.InetAddress.getAllByName0(InetAddress.java:1154)
    at java.net.InetAddress.getAllByName(InetAddress.java:1084)
    at java.net.InetAddress.getAllByName(InetAddress.java:1020)
    at java.net.InetAddress.getByName(InetAddress.java:970)
    at Test.main(Test.java:67)

(I'm a little bit surprised about Inet6AddressImpl here)

As far as I understand is InetAddress.getByName using the native mechanisms to resolve host names. So I don't think that the error is caused by a missing proxy configuration within the java jvm.

But what else can it be, if everything else is working fine?

Some (maybe) useful additional information:

  • I'm using a MacBook, ifconfig shows the interfaces lo0, gif0, stf0, en0, fw0, en1 -> connected to the network, with ipv4 address.

  • nslookup google.com on the console returns ** server can't find google.com: NXDOMAIN

  • The same code on a windows machine within the network produced the same Exception

Any ideas about the cause of this error? Or are there other ways to resolve host names in java?

4

3 回答 3

5

您的公司 DNS 服务器阻止您解析任何 Internet 域(他们可能不希望人们浏览非公司上下文)。

nslookup您的查询失败的事实支持了这一点。

如果您对公司政策没有投票权,并且您的开发机器必须留在公司场所,那么您无能为力。

于 2011-08-02T11:59:54.287 回答
0

本文所述,您可以通过仅 Sun-JVM 设置来设置自定义 DNS。我有同样的解决问题,但能够通过将代理也设置为 DNS 服务器来明确要求代理进行查找。YMMV

System.setProperty("sun.net.spi.nameservice.nameservers", "<my-proxy-ip>");
System.setProperty("sun.net.spi.nameservice.provider.1", "dns,sun");
于 2012-04-09T23:33:39.463 回答
0

我知道这个问题已经更新,但是因为它是谷歌中排名靠前的结果,所以我想添加这条有用的信息,它可能会让人绊倒。

使用此方法时,请记住以下内容。

http://www.google.com -- DOES NOT WORK
google.com -- WORKS!
于 2018-04-17T12:57:44.347 回答