ulmaxy ulmaxy - 5 months ago 61
Java Question

Send HTTP POST request to destination by IP address in java

I have an Apache web server launched at another computer in my local network, that have a .php file in its root folder, so I can send my requests from browser using addresses like "192.168.1.5/connect.php?param1=value1&param2=value2". I really need to do that from my java code, which is kind of confusing for me. Since I'm using IP address, I can't use HttpUrlConnection to send my requests because it requires to have a URL. Is there something similar to HttpUrlConnection but for IP addresses?

Answer

HttpUrlConnection can be perfectly used for both hostname HTTP connections and for IP address HTTP connections:

HttpURLConnection conn = (HttpURLConnection) new URL("http://192.168.1.5/connect.php?param1=value1&param2=value2").openConnection();
Comments