Skip to main content

APIzation: Replication Package

[Q#12364555][A#12381788] How can I display all the HTTP Headers when using the DefaultHTTPClient?

https://stackoverflow.com/q/12364555

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.

Answer

https://stackoverflow.com/a/12381788

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

APIzation

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");
package com.stackoverflow.api;

public class Human12381788 {

  public static void enableDebugLogging() {
    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"
    );
  }
}

package com.stackoverflow.api;

/**
 * How can I display all the HTTP Headers when using the DefaultHTTPClient?
 *
 * @author APIzator
 * @see <a href="https://stackoverflow.com/a/12381788">https://stackoverflow.com/a/12381788</a>
 */
public class APIzator12381788 {

  public static void displayHeaders() throws Exception {
    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"
    );
  }
}