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:
http://localhost:8080/JAXRSJsonCRUDExample/rest/countries
It will give you list of country in json format.
Post URL:
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:
Java code:
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());
}
}
When you run above program, you will get below output:
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}
If you comment http.sendingPostRequest(); and send only get request. You will get below output:
[{"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}]
You can see USA is added to country list.