Table of Contents
In this post, we will see how to send HTTP Get/Post in java.
There are many times when you need to send http get or post request. You can use HttpURLConnection for sending get/post request in java. It belongs to java.net package.
There are many times when you need to send http get or post request. You can use HttpURLConnection for sending get/post request in java. It belongs to java.net package.
HttpURLConnection example:
We are going to send get or post request to URLs used in restful web services CRUD example, so instead of using PostMan , we will send request using java.
Get Url:
1 2 3 |
http://localhost:8080/JAXRSJsonCRUDExample/rest/countries |
It will give you list of country in json format.
Post URL:
1 2 3 4 |
http://localhost:8080/JAXRSJsonCRUDExample/rest/countries Post data:{"id":5,"countryName":"USA","population":8000} |
It will create a new country resource and add it to list of counties.
Java code:
When you run above program, you will get below output:
If you comment http.sendingPostRequest(); and send only get request. You will get below output:
You can see USA is added to country list.
Java code:
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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
package org.arpit.java2blog.corejava; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; /** * @author Arpit Mandliya */ public class HttpURLConnectionExample { private final String USER_AGENT = "Mozilla/5.0"; public static void main(String[] args) throws Exception { HttpURLConnectionExample http = new HttpURLConnectionExample(); // Sending get request http.sendingGetRequest(); // Sending post request http.sendingPostRequest(); } // HTTP GET request private void sendingGetRequest() throws Exception { String urlString = "http://localhost:8080/JAXRSJsonCRUDExample/rest/countries"; URL url = new URL(urlString); HttpURLConnection con = (HttpURLConnection) url.openConnection(); // By default it is GET request con.setRequestMethod("GET"); //add request header con.setRequestProperty("User-Agent", USER_AGENT); int responseCode = con.getResponseCode(); System.out.println("Sending get request : "+ url); System.out.println("Response code : "+ responseCode); // Reading response from input Stream BufferedReader in = new BufferedReader( new InputStreamReader(con.getInputStream())); String output; StringBuffer response = new StringBuffer(); while ((output = in.readLine()) != null) { response.append(output); } in.close(); //printing result from response System.out.println(response.toString()); } // HTTP Post request private void sendingPostRequest() throws Exception { String url = "http://localhost:8080/JAXRSJsonCRUDExample/rest/countries"; URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); // Setting basic post request con.setRequestMethod("POST"); con.setRequestProperty("User-Agent", USER_AGENT); con.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); con.setRequestProperty("Content-Type","application/json"); String postJsonData = "{"id":5,"countryName":"USA","population":8000}"; // Send post request con.setDoOutput(true); DataOutputStream wr = new DataOutputStream(con.getOutputStream()); wr.writeBytes(postJsonData); wr.flush(); wr.close(); int responseCode = con.getResponseCode(); System.out.println("nSending 'POST' request to URL : " + url); System.out.println("Post Data : " + postJsonData); System.out.println("Response Code : " + responseCode); BufferedReader in = new BufferedReader( new InputStreamReader(con.getInputStream())); String output; StringBuffer response = new StringBuffer(); while ((output = in.readLine()) != null) { response.append(output); } in.close(); //printing result from response System.out.println(response.toString()); } } |
1 2 3 4 5 6 7 8 9 10 |
Sending get request : http://localhost:8080/JAXRSJsonCRUDExample/rest/countries Response code : 200 [{"id":1,"countryName":"India","population":10000},{"id":2,"countryName":"Bhutan","population":7000},{"id":3,"countryName":"Nepal","population":8000},{"id":4,"countryName":"China","population":20000}] Sending 'POST' request to URL : http://localhost:8080/JAXRSJsonCRUDExample/rest/countries Post Data : {"id":5,"countryName":"USA","population":8000} Response Code : 200 {"id":5,"countryName":"USA","population":8000} |
1 2 3 4 |
[{"id":1,"countryName":"India","population":10000},{"id":2,"countryName":"Bhutan","population":7000},{"id":3,"countryName":"Nepal","population":8000},{"id":4,"countryName":"China","population":20000}, {"id":5,"countryName":"USA","population":8000}] |
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.