In this post, we will learn “How to send HTTP GET/POST Request in Java”. Here, we are going to use simple HttpURLConnection to do the job.

Http_GET_POST_Request_Java_Techndeck

In this tutorial, we are going to cover below topics:

  1. What is HttpURLConnection?
  2. How to send HTTP GET & POST Request in Java using HttpURLConnection?

Send HTTP GET/POST Request in Java using HttpURLConnection...!!! Click To Tweet

Let’s begin:

1. What is HttpURLConnection?

HttpUrlConnection class is a part of java.net package. It is an abstract class and extends URLConnection class. It provides HTTP specific features alongside all the features acquired by it’s parent class. It allows us to make basic HTTP GET and POST requests.

2. How to send HTTP GET & POST request using HttpURLConnection?

HTTPURLConnection GET Request Example:

Let’s try to understand the code (Step by Step):

1. Specify the URL where we are sending the GET Request and create a URL and Connection object which will open the connection on the specified URL.

2. Once the connection to the specified URL is created, set the set the request method type with setRequestMethod() method.

3. Call setRequestProperty() method to set request headers such as “User-Agent” etc.

4. Check the response code using below line of code.

4. If the response code is OK then create the input stream to read the returned data.

5. Use StringBuffer to build the final string from the response.

6. Now, using below code, we will read the data line by line from the input stream using readLine method and check if it’s not null, then append it to the StringBuffer object to build the final string.

7. Finally, print the response as a string.

HTTPURLConnection POST Request Example:

Let’s try to understand the code (Step by Step):

1. Specify the URL where we are sending the POST Request and create a URL and Connection object which will open the connection on the specified URL.

2. Once the connection to the specified URL is created, set the set the request method type with setRequestMethod() method.

3. Call setRequestProperty() method to set request headers such as “User-Agent” etc.

4. Set setDoOutput to true if we need to send output a request body. This is required in case of POST or PUT requests not the GET.

5. Open the DataOutputStream object

6. Specify the parameters that are required to be posted and assign them to the DataOutputStream object. After that flush the stream and finally close it as it’s job is done.

7. Check the response code using below line of code.

8. If the response code is OK then create the input stream to read the returned data.

9. Use StringBuffer to build the final string from the response.

10. Now, using below code, we will read the data line by line from the input stream using readLine method and check if it’s not null, then append it to the StringBuffer object to build the final string.

11. Finally, print the response as a string.

That’s it, it’s that simple to send Http Get/Post Request in Java 🙂 

Send HTTP GET/POST Request in Java using HttpURLConnection...!!! Click To Tweet

If you like this post, please click like button and share it with others on Twitter. Also, check out my other useful blog posts on Rest Assured:

Other Useful References: