Get IP address with URL string? (Java)

user1205853 picture user1205853 · Feb 15, 2012 · Viewed 59.5k times · Source

In my program a user enters a url string, say

http://www.engineering.uiowa.edu/~hawkeng//fall01/graphics/potato.gif

how would I get the IP address of this url? I tried using

InetAddress address = InetAddress.getByName(urlStr);

but the result always comes back null. What is the proper way to get the IP address?

Answer

Victor Stafusa picture Victor Stafusa · Feb 15, 2012

Try this:

InetAddress address = InetAddress.getByName(new URL(urlString).getHost());

To get the raw IP:

String ip = address.getHostAddress();