Apache Httpclient Read all Headers

 Posted On  | Yashwant Chavan 

In this tutorial you will learn how to read Header values from response using Apache Httpclient. In below example we will prints all the Headers associated with the response.

Maven dependency for Apache httpclient

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

Refer below steps to read response Headers values.

  • Create instance of HttpClient using HttpClientBuilder.create().build()
  • Make a http GET request using HttpGet
  • Execute the request and get the HttpResponse.
  • Once you got the response, read all headers from response object.
  • Iterate and print Header values. Header is used to represents an HTTP header field.
  • You can fetch the specific Header value using header key.

Download File Using HttpCilent

package com.technicalkeeda.http;

import java.io.IOException;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;

public class HttpCilentHeadersExample {

    public static void main(String[] args) throws Exception {
        HttpCilentHeadersExample client = new HttpCilentHeadersExample();
        client.getHeaders();
    }

    public void getHeaders() {
        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("Request Url: " + request.getURI());
            System.out.println("Response Code: " + responseCode);

            Header[] headers = response.getAllHeaders();
            for (Header header: headers) {
                System.out.println("Key [ " + header.getName() + "], Value[ " + header.getValue() + " ]");
            }

            System.out.println("\nRead Specific Header Value");
            System.out.println("Date Header:- " + response.getFirstHeader("Date").getValue());

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

}

output

Request Url: http://www.technicalkeeda.com/get-request
Response Code: 200
Key [ Date], Value[ Sat, 12 Nov 2016 06:31:09 GMT ]
Key [ Server], Value[ Apache Phusion_Passenger/4.0.10 mod_bwlimited/1.4 mod_fcgid/2.3.9 ]
Key [ X-Powered-By], Value[ PHP/5.5.38 ]
Key [ Set-Cookie], Value[ ci_session=a:4:{s:10:"session_id";s:32:"f24ec3be5ec26fa730ee747456ea7576";s:10:"ip_address";s:13:"103.51.93.188";s:10:"user_agent";s:40:"Apache-HttpClient/4.5.2+(Java/1.8.0_102)";s:13:"last_activity";s:10:"1478932270";}61d795f61753587b9015258c91896a76; expires=Sat, 12-Nov-2016 08:31:10 GMT; Max-Age=7200; path=/ ]
Key [ Set-Cookie], Value[ ci_session=a:4:{s:10:"session_id";s:32:"0455f45d7es494ddfgecaac47fc13a00ea3fa3";s:10:"ip_address";s:13:"103.51.93.188";s:10:"user_agent";s:40:"Apache-HttpClient/4.5.2+(Java/1.8.0_102)";s:13:"last_activity";s:10:"1478932270";}81c7f3d0s62e7dd63993a9eef9c2b311cc34; expires=Sat, 12-Nov-2016 08:31:10 GMT; Max-Age=7200; path=/ ]
Key [ Content-Type], Value[ text/html ]
Key [ X-Varnish], Value[ 22093455 ]
Key [ Age], Value[ 0 ]
Key [ Via], Value[ 1.1 varnish-v4 ]
Key [ Accept-Ranges], Value[ bytes ]
Key [ Content-Length], Value[ 78 ]

Read Specific Header Value
Date Header:- Sat, 12 Nov 2016 06:31:09 GMT


© technicalkeeda.com 2017

 |  Find us on Google+ |  Rss Feed

Loaded in 0.0302 seconds.