How can I display all HTTP headers when using DefaultHTTPClient?

When using DefaultHttpClient() from the Apache Commons HTTP client, you can display the full request at the console output for debugging purposes.

I am having problems with my application and I think the easiest way to debug it is to check all the data sent by DefaultHTTPClient

thanks

+6
source share
5 answers

From another answer on StackOverflow. This can be easily done by turning on 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"); 
+7
source

you can get all the headers like this

 Enumeration headerNames = request.getHeaderNames(); while(headerNames.hasMoreElements()) { String headerName = (String)headerNames.nextElement(); out.println("" + headerName); out.println("" + request.getHeader(headerName)); } 

see this tutorial http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-Request-Headers.html for more information

+12
source

Here is a 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()); } 
+5
source

When you execute the request, you pass the HttpRequest object. It has a getAllHeaders() method

0
source

If you use Logback as your logging structure, add the following configuration to your logback.xml / logback-test.xml :

 <?xml version="1.0" encoding="UTF-8"?> <configuration scan="true"> <!-- more config --> <logger name="org.apache.http" level="DEBUG"/> <!-- more config --> </configuration> 

By adding this configuration, log journal applications will now display useful HttpClient-related information about HTTP requests and response headers, among other things.

0
source

Source: https://habr.com/ru/post/925095/


All Articles