2014-09-01 124 views
0

我在java中发出http post请求,我需要查看请求中发送的xml。 有没有办法在java中看到xml?请提供打印发送的请求xml的方法。在Java中如何提取通过java发送的Http Post请求中的xml?

示例代码:

public static void main(String[] args) { 
    // TODO Auto-generated method stub 
     HttpClient cl = new HttpClient(); 
     PostMethod postMethod = new PostMethod("***URL***"); 

     NameValuePair[] params = { 
     new NameValuePair("dealerId", "***sampleDealerId***"), 
     new NameValuePair("queryId", "***sampleQueryId***") 
     }; 

     postMethod.setRequestBody(params); 
     cl.getParams().setAuthenticationPreemptive(true); 
     Credentials defaultCreds = new UsernamePasswordCredentials("***user***", "***password***"); 
     cl.getState().setCredentials(AuthScope.ANY, defaultCreds); 

     try { 
      try { 
      cl.executeMethod(postMethod); 
      } 
      catch (IOException e) { 
      e.printStackTrace(); 
      } 
      BufferedReader br; 

      try { 
      System.out.println(postMethod.getStatusCode()); 
      System.out.println(postMethod.getStatusText()); 
      br = new BufferedReader(new InputStreamReader(postMethod.getResponseBodyAsStream()),256); 
       String line; 
       char[] chars = new char[256]; 
       while (br.read(chars) != -1) { 
        System.out.println(chars); 
        chars = new char[256]; 
       } 
      } 
      catch (IOException e) { 
      e.printStackTrace(); 
      } 
     } 
     finally { 
     postMethod.releaseConnection(); 
     } 

} 

回答

0

设置以下的记录参数应实现的全线速内容的日志记录(其中应包括XML你发送)和上下文。它意味着与接口共同工作。

-Dorg.apache.commons.logging.Log = org.apache.commons.logging.impl.SimpleLog -Dorg.apache.commons.logging.simplelog.showdatetime =真 -Dorg.apache.commons .logging.simplelog.log.org.apache.http = DEBUG

相同水平的调试的用于log4j的

log4j.rootLogger = INFO,由于输出

log4j.appender.stdout = org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout = org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern =%5P [%C]%米%正

log4j.logger.org.apache.http = DEBUG

More info

编辑响应于从OP评价:

Ť他上面链接显示完整的例子。这取决于你正在使用哪个日志框架。例如,如果你使用log4j的,一个非常普遍的选择进行日志记录,您将创建一个log4j.properties文件与指定log4j的配置参数(我上面指定的第二块)。

一旦日志设置生效,就不需要在代码中明确地打印任何内容。假设正确的设置就位,Apache HTTP代码中的现有调试日志记录应记录响应/请求等。

+0

嘿,你可以请共享更多细节如何以及在Commons Logging Interface中设置日志参数的位置?还需要在程序中打印什么来获取请求xml? – Sapan 2014-09-01 08:17:24