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.
In this tutorial, we are going to cover below topics:
- What is HttpURLConnection?
- 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class HttpURLConnection_GET_Request_Example { public static void main(String[] args) throws Exception { String url = "http://www.google.com/search?q=techndeck"; URL urlObj = new URL(url); HttpURLConnection connection = (HttpURLConnection) urlObj.openConnection(); connection.setRequestMethod("GET"); connection.setRequestProperty("User-Agent", "Mozilla/5.0"); System.out.println("Send 'HTTP GET' request to : " + url); Integer responseCode = connection.getResponseCode(); System.out.println("Response Code : " + responseCode); if (responseCode == HttpURLConnection.HTTP_OK) { BufferedReader inputReader = new BufferedReader( new InputStreamReader(connection.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = inputReader.readLine()) != null) { response.append(inputLine); } inputReader.close(); System.out.println(response.toString()); } } } |
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.
1 2 3 | String url = "http://www.google.com/search?q=techndeck"; URL urlObj = new URL(url); HttpURLConnection connection = (HttpURLConnection) urlObj.openConnection(); |
2. Once the connection to the specified URL is created, set the set the request method type with setRequestMethod()
method.
1 | connection.setRequestMethod("GET"); |
3. Call setRequestProperty() method to set request headers such as “User-Agent” etc.
1 | connection.setRequestProperty("User-Agent", "Mozilla/5.0"); |
4. Check the response code using below line of code.
1 | Integer responseCode = connection.getResponseCode(); |
4. If the response code is OK then create the input stream to read the returned data.
1 | BufferedReader inputReader = new BufferedReader( new InputStreamReader(connection.getInputStream())); |
5. Use StringBuffer to build the final string from the response.
1 | StringBuffer response = new StringBuffer(); |
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.
1 | while ((inputLine = inputReader.readLine()) != null) { response.append(inputLine); } |
7. Finally, print the response as a string.
1 | response.toString() |
HTTPURLConnection POST Request Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class HttpURLConnection_Post_Request_Example { String url = "http://localhost:8006/SpringBootServiceProject/Users"; URL UrlObj = new URL(url); HttpURLConnection connection = (HttpURLConnection) UrlObj.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("User-Agent", "Mozilla/5.0"); connection.setDoOutput(true); DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream()); String urlPostParameters = "userID=250&userName=Mike"; outputStream.writeBytes(urlPostParameters); outputStream.flush(); outputStream.close(); System.out.println("Send 'HTTP POST' request to : " + url); int responseCode = connection.getResponseCode(); System.out.println("Response Code : " + responseCode); if (responseCode == HttpURLConnection.HTTP_OK) { BufferedReader inputReader = new BufferedReader( new InputStreamReader(connection.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = inputReader.readLine()) != null) { response.append(inputLine); } inputReader.close(); System.out.println(response.toString()); } } } |
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.
1 2 3 | String url = "http://localhost:8006/SpringBootServiceProject/Users"; URL UrlObj = new URL(url); HttpURLConnection connection = (HttpURLConnection) UrlObj.openConnection(); |
2. Once the connection to the specified URL is created, set the set the request method type with setRequestMethod()
method.
1 | connection.setRequestMethod("POST"); |
3. Call setRequestProperty() method to set request headers such as “User-Agent” etc.
1 | connection.setRequestProperty("User-Agent", "Mozilla/5.0"); |
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.
1 | connection.setDoOutput(true); |
5. Open the DataOutputStream object
1 | DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream()); |
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.
1 2 3 4 | String urlPostParameters = "userID=250&userName=Mike"; outputStream.writeBytes(urlPostParameters); outputStream.flush(); outputStream.close(); |
7. Check the response code using below line of code.
1 | Integer responseCode = connection.getResponseCode(); |
8. If the response code is OK then create the input stream to read the returned data.
1 | BufferedReader inputReader = new BufferedReader( new InputStreamReader(connection.getInputStream())); |
9. Use StringBuffer to build the final string from the response.
1 | StringBuffer response = new StringBuffer(); |
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.
1 | while ((inputLine = inputReader.readLine()) != null) { response.append(inputLine); } |
11. Finally, print the response as a string.
1 | response.toString() |
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:
I admire this article for the well-researched content and excellent wording
Thank you so much for appreciating the work.