Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I display all the HTTP Headers when using the DefaultHTTPClient?

When using the DefaultHttpClient() from the Apache Commons HTTP Client, is it possible to show the full request in the console output for debugging purposes?

I'm having issues with my application and I feel that the easiest way to debug it it would be to inspect all data sent by the DefaultHTTPClient.

like image 832
Mridang Agarwalla Avatar asked Sep 11 '12 07:09

Mridang Agarwalla


Video Answer


2 Answers

From another answer on StackOverflow. This can easily be done by enabling the debug logging for the Apache HTTP Client:

java.util.logging.Logger.getLogger("org.apache.http.wire").setLevel(java.util.logging.Level.FINEST);
java.util.logging.Logger.getLogger("org.apache.http.headers").setLevel(java.util.logging.Level.FINEST);

System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");
System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true");
System.setProperty("org.apache.commons.logging.simplelog.log.httpclient.wire", "debug");
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http", "debug");
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.headers", "debug");
like image 72
Mridang Agarwalla Avatar answered Sep 20 '22 12:09

Mridang Agarwalla


Yes, here's sample code:

    import java.util.Arrays;
    import org.apache.http.Header;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    ...
    HttpResponse response;
    ...
    HttpGet httpGet = new HttpGet(serviceURL);
    response = httpclient.execute(httpGet);
    ...
    // Print all headers
    List<Header> httpHeaders = Arrays.asList(response.getAllHeaders());        
    for (Header header : httpHeaders) {
        System.out.println("Headers.. name,value:"+header.getName() + "," + header.getValue());
    }
like image 20
Suren Konathala Avatar answered Sep 18 '22 12:09

Suren Konathala