Friday 23 October 2015

HTTP Client: Working with message headers

HttpClient provides methods to retrieve, add, remove and enumerate headers.
Following are the methods specified in ‘HttpMessage’ interface, to work with headers.


Method
Description
Header[] getAllHeaders()
Return all header for this message.
Header[] getHeaders(String name)
Return all header with a specified name of this message.
void setHeader(Header header)
Overwrites the first header with the same name. The new header will be appended to the end of the list, if no header with the given name can be found.
void setHeader(String name, String value)
Overwrites the first header with the same name. The new header will be appended to the end of the list, if no header with the given name can be found.
void setHeaders(Header[] headers)
Overwrites all the headers in the message.
void addHeader(Header header)
Adds a header to this message. The header will be appended to the end of the list.
void addHeader(String name, String value)
Adds a header to this message. The header will be appended to the end of the list.
HeaderIterator headerIterator()
Returns an iterator of all the headers.
HeaderIterator headerIterator(String name)
Returns an iterator of the headers with a given name.

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.http.Header;
import org.apache.http.HeaderIterator;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

public class Main {
  public static void printResponse(HttpResponse response) {
    System.out.println(response.getProtocolVersion());
    System.out.println(response.getStatusLine().getStatusCode());
    System.out.println(response.getStatusLine().getReasonPhrase());
    System.out.println(response.getStatusLine().toString());

    HeaderIterator iter = response.headerIterator();

    while (iter.hasNext()) {
      Header header = iter.nextHeader();
      System.out.println(header.getName() + " " + header.getValue());
    }
  }

  public static void main(String args[]) throws ClientProtocolException,
      IOException, URISyntaxException {
    URI uri = new URIBuilder().setScheme("http").setHost("www.google.com")
        .setPath("/search")
        .setParameter("q", "self learning java blog").build();

    System.out.println(uri);

    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpGet httpget = new HttpGet(uri);
    CloseableHttpResponse response = httpclient.execute(httpget);

    printResponse(response);

    response.close();
  }
}


Output
http://www.google.com/search?q=self+learning+java+blog
HTTP/1.1
200
OK
HTTP/1.1 200 OK
Date Wed, 07 Oct 2015 07:04:47 GMT
Expires -1
Cache-Control private, max-age=0
Content-Type text/html; charset=ISO-8859-1
P3P CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."
Server gws
X-XSS-Protection 1; mode=block
X-Frame-Options SAMEORIGIN
Transfer-Encoding chunked
Set-Cookie PREF=ID=1111111111111111:FF=0:TM=1444201487:LM=1444201487:V=1:S=I-vA0uxtAgFIestH; expires=Thu, 31-Dec-2015 16:02:17 GMT; path=/; domain=.google.com
Set-Cookie NID=72=sG2UkTHrMN5i4t_GtDYAeLv9PaBsA8dqDPAP1E5iVdwOaOBFoM3PKPHqcuATk5dvWghvZ2E7PQ4rVazfJzEn9JK02Z7rsVj4egJY1HoJEUiKKBOKR84EXN5jYrDMHM_R6iCMN25ZxOnAEsO4Avpd3WMArcqfqgkp; expires=Thu, 07-Apr-2016 07:04:47 GMT; path=/; domain=.google.com; HttpOnly







Prevoius                                                 Next                                                 Home

No comments:

Post a Comment