InetAddress.getByName fails behind proxy

Benjamin picture Benjamin · Aug 2, 2011 · Viewed 7.4k times · Source

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?

Answer

Alexander Pogrebnyak picture Alexander Pogrebnyak · Aug 2, 2011

Your corporate DNS server prevents you from resolving any Interent domain ( they probably do not want people browsing non-corporate context ).

This is supported by the fact that your nslookup query fails.

If you don't have a vote on your corporate policy, and your development machine has to stay on your company premises, there is nothing that you can do.