Apache HttpClient Examples

This article shows you how to use Apache HttpClient to send HTTP GET/POST request.

1. Send HTTP GET Request

HttpClient to send a HTTP GET request to get the Google’s search result.

	String url = "http://www.google.com/search?q=httpClient";
	HttpClient client = HttpClientBuilder.create().build();
	HttpGet request = new HttpGet(url);
	// add request header
	request.addHeader("User-Agent", USER_AGENT);
	HttpResponse response = client.execute(request);
	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);

2. Send HTTP POST Request

HttpClient to send an HTTP POST request to Apple.com search form.

	String url = "https://selfsolve.apple.com/wcResults.do";
	HttpClient client = HttpClientBuilder.create().build();
	HttpPost post = new HttpPost(url);
	// add header
	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("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);

3. Apache HttpClient – Automate login Google

A full example to automate login to Gmail.

  1. Send a GET request to get login form.
  2. Uses jsoup html parser to grab form inputs.
  3. Constructs parameters and make a POST request for authentication.
  4. Send another GET request to Gmail.
HttpCilentExample.java
package com.mkyong;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.CookieHandler;
import java.net.CookieManager;
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.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class HttpCilentExample {
  private String cookies;
  private HttpClient client = HttpClientBuilder.create().build();
  private final String USER_AGENT = "Mozilla/5.0";
  public static void main(String[] args) throws Exception {
	String url = "https://accounts.google.com/ServiceLoginAuth";
	String gmail = "https://mail.google.com/mail/";
	// make sure cookies is turn on
	CookieHandler.setDefault(new CookieManager());
	HttpCilentExample http = new HttpCilentExample();
	String page = http.GetPageContent(url);
	List<NameValuePair> postParams = 
               http.getFormParams(page, "username","password");
	http.sendPost(url, postParams);
	String result = http.GetPageContent(gmail);
	System.out.println(result);
	System.out.println("Done");
  private void sendPost(String url, List<NameValuePair> postParams) 
        throws Exception {
	HttpPost post = new HttpPost(url);
	// add header
	post.setHeader("Host", "accounts.google.com");
	post.setHeader("User-Agent", USER_AGENT);
	post.setHeader("Accept", 
             "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
	post.setHeader("Accept-Language", "en-US,en;q=0.5");
	post.setHeader("Cookie", getCookies());
	post.setHeader("Connection", "keep-alive");
	post.setHeader("Referer", "https://accounts.google.com/ServiceLoginAuth");
	post.setHeader("Content-Type", "application/x-www-form-urlencoded");
	post.setEntity(new UrlEncodedFormEntity(postParams));
	HttpResponse response = client.execute(post);
	int responseCode = response.getStatusLine().getStatusCode();
	System.out.println("\nSending 'POST' request to URL : " + url);
	System.out.println("Post parameters : " + postParams);
	System.out.println("Response Code : " + responseCode);
	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());
  private String GetPageContent(String url) throws Exception {
	HttpGet request = new HttpGet(url);
	request.setHeader("User-Agent", USER_AGENT);
	request.setHeader("Accept",
		"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
	request.setHeader("Accept-Language", "en-US,en;q=0.5");
	HttpResponse response = client.execute(request);
	int responseCode = response.getStatusLine().getStatusCode();
	System.out.println("\nSending 'GET' request to URL : " + url);
	System.out.println("Response Code : " + responseCode);
	BufferedReader rd = new BufferedReader(
                new InputStreamReader(response.getEntity().getContent()));
	StringBuffer result = new StringBuffer();
	String line = "";
	while ((line = rd.readLine()) != null) {
		result.append(line);
	// set cookies
	setCookies(response.getFirstHeader("Set-Cookie") == null ? "" : 
                     response.getFirstHeader("Set-Cookie").toString());
	return result.toString();
  public List<NameValuePair> getFormParams(
             String html, String username, String password)
			throws UnsupportedEncodingException {
	System.out.println("Extracting form's data...");
	Document doc = Jsoup.parse(html);
	// Google form id
	Element loginform = doc.getElementById("gaia_loginform");
	Elements inputElements = loginform.getElementsByTag("input");
	List<NameValuePair> paramList = new ArrayList<NameValuePair>();
	for (Element inputElement : inputElements) {
		String key = inputElement.attr("name");
		String value = inputElement.attr("value");
		if (key.equals("Email"))
			value = username;
		else if (key.equals("Passwd"))
			value = password;
		paramList.add(new BasicNameValuePair(key, value));
	return paramList;
  public String getCookies() {
	return cookies;
  public void setCookies(String cookies) {
	this.cookies = cookies;
Note
You may also interest at this equivalent example – automate login Gmail with Java HttpsURLConnection

References

  1. Apache HttpClient
  2. jsoup library
  3. Automate login Gmail with Java HttpsURLConnection

上一篇: How to send HTTP request GET/POST in Java
下一篇: How to automate login a website Java example
 评论 ( What Do You Think )
名称
邮箱
网址
评论
验证
   
 

 

一号门极客CMS,由Python,Django,MySQL,Apache,Mod Wsgi 强力驱动.
鄂ICP备14001754号-3