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:
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("http://151.101.65.69");
and opening a stream to this URL (a
) 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");
works. - At this point I am stuck. I have no Idea what to change, or how to move forward.
- 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:
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 151.101.65.69 (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.
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?