HttpURLConnection class from java.net package can be used to send Java HTTP Request programmatically. Today we will learn how to use HttpURLConnection in java program to send GET and POST requests and then print the response.

Java HTTP Request

For our HttpURLConnection example, I am using sample project from Spring MVC Tutorial because it has URLs for GET and POST HTTP methods. Below are the images for this web application, I have deployed it on my localhost tomcat server.

Java HTTP GET Request

Spring-MVC-HelloWorld (2)

Java HTTP GET Request for Login Page

Spring-MVC-HelloWorld-GET

Java HTTP POST Request

Spring-MVC-HelloWorld-GET

For Java HTTP GET requests, we have all the information in the browser URL itself. So from the above images, we know that we have the following GET request URLs.

  • https://localhost:9090/SpringMVCExample/
  • https://localhost:9090/SpringMVCExample/login

Above URL’s don’t have any parameters but as we know that in HTTP GET requests parameters are part of URL itself, so for example if we have to send a parameter named userName with value as Pankaj then the URLs would have been like below.

  • https://localhost:9090/SpringMVCExample?userName=Pankaj
  • https://localhost:9090/SpringMVCExample/login?userName=Pankaj&pwd=apple123 – for multiple params

If we know the POST URL and what parameters it’s expecting, it’s awesome but in this case, we will figure it out from the login form source.

Below is the HTML code we get when we view the source of the login page in any of the browsers.

Look at the form method in the source, as expected it’s POST method. Now see that action is “home”, so the POST URL would be https://localhost:9090/SpringMVCExample/home. Now check the different elements in the form, from the above form we can deduce that we need to send one POST parameter with name userName and it’s of type String.

So now we have complete details of the GET and POST requests and we can proceed for the Java HTTP Request example program.

Below are the steps we need to follow for sending Java HTTP requests using HttpURLConnection class.

  1. Create URL object from the GET/POST URL String.
  2. Call openConnection() method on URL object that returns instance of HttpURLConnection
  3. Set the request method in HttpURLConnection instance, default value is GET.
  4. Call setRequestProperty() method on HttpURLConnection instance to set request header values, such as “User-Agent” and “Accept-Language” etc.
  5. We can call getResponseCode() to get the response HTTP code. This way we know if the request was processed successfully or there was any HTTP error message thrown.
  6. For GET, we can simply use Reader and InputStream to read the response and process it accordingly.
  7. For POST, before we read response we need to get the OutputStream from HttpURLConnection instance and write POST parameters into it.

HttpURLConnection Example

Based on the above steps, below is the example program showing usage of HttpURLConnection to send Java GET and POST requests.

HttpURLConnectionExample.java code:

When we execute the above program, we get below response.

Just compare it with the browser HTTP response and you will see that it’s same. You can also save response into any HTML file and open it to compare the responses visually.

Quick Tip: If you have to send GET/POST requests over HTTPS protocol, then all you need is to use javax.net.ssl.HttpsURLConnection instead of java.net.HttpURLConnection. Rest all the steps will be same as above, HttpsURLConnection will take care of SSL handshake and encryption.

By admin

Leave a Reply

%d bloggers like this: