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?
We can use InetAddressValidator class that provides the following validation methods to validate an IPv4 or IPv6 address. isValid(inetAddress) : Returns true if the specified string is a valid IPv4 or IPv6 address. isValidInet4Address(inet4Address) : Returns true if the specified string is a valid IPv4 address.
In Java, you can use HttpServletRequest. getRemoteAddr() to get the client's IP address that's accessing your Java web application.
Try this:
InetAddress address = InetAddress.getByName(new URL(urlString).getHost());
To get the raw IP:
String ip = address.getHostAddress();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With