Apache HttpClient GET/POST Request Examples

 Posted On  | Yashwant Chavan 

In this tutorial you will learn how to make GET / POST request to server using Apache HttpClient in Java. I am using Apache HttpClient version 4.5.2. Below example describe how to retrieve and send request data using HttpClient.

Refer below steps.

HttpCilent GET Request

  • Create instance of HttpClient using HttpClientBuilder.create().build().
  • Create http GET request using HttpGet
  • Execute the Request - Executes HTTP GET request using HttpClient instance.
  • Get and print the response data on console

HttpCilent POST Request

  • Create instance of HttpClient using HttpClientBuilder.create().build().
  • Create http POST request using HttpPost
  • Post Parameters - Post the request parameters to server using List of NameValuePair. Like in web application we use send data using FORM submit or using Ajax request.
  • Execute the Request - Executes HTTP POST request using HttpClient instance.
  • Get and print the response data on console

Maven dependency for Apache httpclient

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.2</version>
</dependency>

HttpCilent GET / POST Request Example

package com.technicalkeeda.http;

import java.io.BufferedReader;
import java.io.IOException;
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.ClientProtocolException;
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;

public class HttpCilentExample {

    public static void main(String[] args) throws Exception {
        HttpCilentExample client = new HttpCilentExample();
        System.out.println("Http GET Request Example\n");
        client.get();
        System.out.println("\nHttp POST Request Example\n");
        client.post();
    }

    public void get() {
        try {
            HttpClient client = HttpClientBuilder.create().build();
            HttpGet request = new HttpGet("http://www.technicalkeeda.com/get-request");
            HttpResponse response = client.execute(request);

            int responseCode = response.getStatusLine().getStatusCode();

            System.out.println("**GET** request Url: " + request.getURI());
            System.out.println("Response Code: " + responseCode);
            System.out.println("Content:-\n");
            BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

            String line = "";
            while ((line = rd.readLine()) != null) {
                System.out.println(line);
            }

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (UnsupportedOperationException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void post() {
        HttpClient client = HttpClientBuilder.create().build();
        HttpPost post = new HttpPost("http://www.technicalkeeda.com/post-request");
        try {
            List <NameValuePair> nameValuePairs = new ArrayList <NameValuePair> ();
            nameValuePairs.add(new BasicNameValuePair("name", "Yashwant"));
            post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            HttpResponse response = client.execute(post);

            int responseCode = response.getStatusLine().getStatusCode();
            System.out.println("**POST** request Url: " + post.getURI());
            System.out.println("Parameters : " + nameValuePairs);
            System.out.println("Response Code: " + responseCode);
            System.out.println("Content:-\n");
            BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            String line = "";
            while ((line = rd.readLine()) != null) {
                System.out.println(line);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

output

Http GET Request Example

**GET** request Url: http://www.technicalkeeda.com/get-request
Response Code: 200
Content:-

<html><head><title>GET Request Example<title></head><body>Thanks</body></html>

Http POST Request Example

**POST** request Url: http://www.technicalkeeda.com/post-request
Parameters : [name=Yashwant]
Response Code: 200
Content:-

<html><head><title>POST Request Example<title></head><body>Hello Yashwant</body></html>


© technicalkeeda.com 2017

 |  Find us on Google+ |  Rss Feed

Loaded in 0.0329 seconds.