How To Send HTTP Request GET/POST In Java Using Apache Client

Apache provides an HttpClient library using which we can send get and post requests to a server. Another method for http requests is provided by the Standard HttpUrlConnection API provided java but in this article we will use the Apache HttpClient library for GET and POST requests.

 

Standard HttpURLConnection is discussed here.

However in this article, we use the Apache HttpClient library for GET and POST requests.

 

Apache HttpClient

HttpClientExample.java

package com.mudassirshahzad;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

public class HttpClientExample {

private final String USER_AGENT = "Mozilla/5.0";

public static void main(String[] args) throws Exception {

HttpClientExample http = new HttpClientExample();

System.out.println("Testing 1 - Send Http GET request");
http.sendGet();

System.out.println("\nTesting 2 - Send Http POST request");
http.sendPost();

}

// HTTP GET request
private void sendGet() throws Exception {

String url = "http://www.google.com/search?q=developer";

HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);

// add request header
request.addHeader("User-Agent", USER_AGENT);

HttpResponse response = client.execute(request);

System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " +
response.getStatusLine().getStatusCode());

BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));

StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}

System.out.println(result.toString());

}

}

Similarly for POST request, add this method

// HTTP POST request
private void sendPost() throws Exception {

String url = "https://selfsolve.apple.com/wcResults.do";

HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);

post.setHeader("User-Agent", USER_AGENT);

List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
urlParameters.add(new BasicNameValuePair("sn", "C02G8416DRJM"));
urlParameters.add(new BasicNameValuePair("cn", ""));
urlParameters.add(new BasicNameValuePair("locale", ""));
urlParameters.add(new BasicNameValuePair("caller", ""));
urlParameters.add(new BasicNameValuePair("num", "12345"));

post.setEntity(new UrlEncodedFormEntity(urlParameters));

HttpResponse response = client.execute(post);
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + post.getEntity());
System.out.println("Response Code : " + response.getStatusLine().getStatusCode());

BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}

System.out.println(result.toString());

}

This is an equivalent example of Standard HttpUrlConnection, but this time, we are using Apache HttpClient to make HTTP GET/POST request.

References

  1. Apache HttpClient

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top