Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

InetAddress.getByName fails behind proxy

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?

like image 886
Benjamin Avatar asked Aug 02 '11 11:08

Benjamin


People also ask

What exceptions can InetAddress getByName () throw?

Throws. UnknownHostException: if no IP address for the host could be found, or if a scope_id was specified for a global IPv6 address. SecurityException: if a security manager exists and its checkConnect method doesn?t allow the operation.

What is InetAddress getLocalHost ()?

public static InetAddress getLocalHost() throws UnknownHostException. Returns the address of the local host.

What is InetAddress?

InetAddress class is a representation of an IP address. It represents both the 32-bit IPv4 address and the 128-bit IPv6 address. It is the superclass of Inet6Address and Inet4Address classes.

Which are the factory methods for InetAddress?

Factory Methods are static methods in a class that return an object of that class. This method returns the instance of InetAddress containing the local hostname and address. This method returns the instance of InetAddress containing LocalHost IP and name.


1 Answers

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.

like image 95
Alexander Pogrebnyak Avatar answered Oct 02 '22 17:10

Alexander Pogrebnyak