如何通过java提取Http Post请求中发送的xml?

如何通过java提取Http Post请求中发送的xml?,java,xml,http,https,Java,Xml,Http,Https,我正在用java发出http post请求,我需要查看请求中发送的xml。 有没有办法在java中查看xml?请提供打印发送的请求xml的方法 java中的示例代码: public static void main(String[] args) { // TODO Auto-generated method stub HttpClient cl = new HttpClient(); PostMethod postMethod = new PostMethod

我正在用java发出http post请求,我需要查看请求中发送的xml。 有没有办法在java中查看xml?请提供打印发送的请求xml的方法

java中的示例代码:

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

}

设置以下日志参数应启用完整wire内容的日志记录,其中应包括您发送的XML和上下文。它的目的是使用Commons日志接口

-Dorg.apache.commons.logging.Log=org.apache.commons.logging.impl.SimpleLog -Dorg.apache.commons.logging.simplelog.showdatetime=true -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.patternalyout log4j.appender.stdout.layout.ConversionPattern=%5p[%c]%m%n

log4j.logger.org.apache.http=DEBUG

根据OP的评论进行编辑:

上面的链接显示了完整的示例。这取决于您使用的日志框架。例如,如果您正在使用log4j,这是一个非常常见的日志选择,那么您将创建一个log4j.properties文件,其中包含我在上面指定的第二个块中指定的log4j配置参数


一旦日志设置生效,就不需要在代码中显式打印任何内容。Apache HTTP代码中现有的调试日志应该记录响应/请求,以及其他内容,前提是设置了正确的设置。

Hey,您能分享更多详细信息吗?如何以及在Commons日志界面中设置日志参数的位置?另外,需要在程序中打印什么才能获得请求xml?