2012-09-11 22 views

Respuesta

7

De otra respuesta en StackOverflow. Esto se puede hacer fácilmente, permitiendo el registro de depuración del cliente HTTP Apache:

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"); 
+0

He insertado este código, pero no obtengo ningún registro extra de esto. ¿Hay algo más que deba hacer para que esto funcione? – Radon8472

+0

¿Deberíamos ponerlo en las propiedades de gradle? T____T – Isabelle

0

Al ejecutar la solicitud está de paso en algún lugar objeto HttpRequest. Tiene método getAllHeaders()

5

He aquí un ejemplo de código:

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()); 
    } 
0

Si está utilizando Logback como su marco de registro, agregue la siguiente configuración para su archivo 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> 

Después de haber añadido esta configuración, Logback de Los anexos de registro ahora mostrarán información útil relacionada con HttpClient sobre los encabezados de solicitud y respuesta HTTP, entre otras cosas.

Cuestiones relacionadas