Apache HttpClient GET and POST examples
The Apache HttpClient library allows to handling HTTP requests.
In this tutorial we will see GET and POST methods with examples
Maven dependency
org.apache.httpcomponents httpclient 4.1.1
Apache HttpClient with GET API
Java program for how to send json data using http get request.
public static void demoGetRESTAPI() throws Exception { DefaultHttpClient httpClient = new DefaultHttpClient(); try { //Define a HttpGet request; You can choose between HttpPost, HttpDelete or HttpPut also. //Choice depends on type of method you will be invoking. HttpGet getRequest = new HttpGet("https://run.mocky.io/v3/8ca4a60c-7bae-440f-94f6-909fceb75d5a"); //Set the API media type in http accept header getRequest.addHeader("accept", "application/xml"); //Send the request; It will immediately return the response in HttpResponse object HttpResponse response = httpClient.execute(getRequest); //verify the valid error code first int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != 200) { throw new RuntimeException("Failed with HTTP error code : " + statusCode); } //Now pull back the response object HttpEntity httpEntity = response.getEntity(); String apiOutput = EntityUtils.toString(httpEntity); //Lets see what we got from API System.out.println(apiOutput); //
//In realtime programming, you will need to convert this http response to some java object to re-use it. //Lets see how to jaxb unmarshal the api response content JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); Customer customer = (Customer) jaxbUnmarshaller.unmarshal(new StringReader(apiOutput)); //Verify the populated object System.out.println(customer.getId()); System.out.println(customer.getName()); System.out.println(customer.getPhone()); System.out.println(customer.getAddress)); System.out.println(customer.getCity()); System.out.println(customer.getCountry()); System.out.println(customer.getPin()); } finally { //Important: Close the connect httpClient.getConnectionManager().shutdown(); } } demo user
Apache HttpClient with POST API
Java program for how to send json data to server using http post request.
public static void AddCustomer() throws Exception
{
DefaultHttpClient httpClient = new DefaultHttpClient();
Customer customer = new Customer();
customer.setId(2);
customer.setName("Bala");
customer.setPhone("0987654321");
customer.setAddress("xyz");
customer.setCity("Mumbai");
customer.setCountry("India");
customer.setPin(400702);
StringWriter writer = new StringWriter();
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.marshal(user, writer);
try
{
//Define a postRequest request
HttpPost postRequest = new HttpPost("http://localhost:8080/customer
//Set the API media type in http content-type header
postRequest.addHeader("content-type", "application/xml");
//Set the request post body
StringEntity userEntity = new StringEntity(writer.getBuffer().toString());
postRequest.setEntity(userEntity);
//Send the request; It will immediately return the response in HttpResponse object if any
HttpResponse response = httpClient.execute(postRequest);
//verify the valid error code first
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != 200)
{
throw new Exception("Failed with HTTP error code : " + statusCode);
}
}
finally
{
//Important: Close the connect
httpClient.getConnectionManager().shutdown();
}
}
In this article, we have seen Apache HttpClient GET and POST examples . All source code in the article can be found in the GitHub repository.
0 Comments
Post a Comment