Mike Mike - 1 year ago 103
HTTP Question

how to create a URL object using an InetAddress object in Java?

I have a need to create a http or https URL object from an IPv4 address (String or InetAddress objects - either one is ok) in Java. I have been at this for 10 hours now.

Attempts that hit a wall described below:

  1. Attempt #1: I tried to make the URL by assembling a string, and then feeding it to a URL constructor.

    • Textbook states that a URL can be "protocol://host", with host being either a host name or IP address. but creating a URL like this:
      URL a = new URL("");
      and opening a stream to this URL (
      ) gives a HTTP error 500 (Internal Server Error - An unexpected condition occurred that the server does not know how to handle).

    • What get me fuming is that
      URL a = new URL("http://stackoverflow.com");

    • At this point I am stuck. I have no Idea what to change, or how to move forward.

  2. Attempt #2: I tried to do a reverse lookup on the IP address using "getHostName()" method in the InetAddress class.

    • this should return the host name by doing a reverse DNS lookup. Yet, I keep trying it for (stackoverflow web server IP address), and the look up fails. By fails I mean the IP address is returned as string rather than the host name as a string. I read the Oracle docs http://docs.oracle.com/javase/1.5.0/docs/api/java/net/InetAddress.html#getHostName(), but I don't understand how to overcome the "security manager" the document mentions (or if it is indeed the reason the reverse lookup fails).

    • I also tried "getCannonicalHostName()", but that didn't fly either.

  3. I am trying to figure out how to open a website using the IP address. It looks like my browser is running into the same issue as my code. I read up on How to access site through IP address when website is on a shared host? but I do not have any user names, as I want to be able to open any website that a user has an IP address for. Adding a port (such as 80) does not seem to work; neither does leaving the user name blank or using a generic 'user' or 'guest'.

I need is to create a URL object from an IPv4 String or InetAddress object, and I am stuck. I understand that a knowledgeable programmer such as you, may say that making URLs from IP addresses is not what IP addresses are for, or point out that I am not including a file portion of the URL, but that is not the problem at this moment. Could you please help me with my core challenge?

Answer Source

The following code works for me.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;

public class InetAddressMain {

    public static void main(String[] args) {
        try {
            InetAddress addr = InetAddress.getByName("");

            URL url = new URL("http://"+addr.getHostAddress());
            InputStream is = url.openStream();

            InputStreamReader isReader = new InputStreamReader(is);
            BufferedReader reader = new BufferedReader(isReader);
            String line;
            while((line = reader.readLine()) != null){

        } catch (UnknownHostException e) {
        } catch (MalformedURLException e) {
        } catch (IOException e) {



<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="en"><head><meta content="Search the world's information, including webpages, images, videos and more. Google has many special features to help you find exactly what you're looking for." ... [output shortened for readability]

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download