简体   繁体   English

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

[英]How to extract the xml sent in a Http Post request through java?

I am making a http post request in java and I need to see the xml which is sent in the request. 我正在用Java发出http发布请求,我需要查看请求中发送的xml。 Is there a way to see that xml in java ? 有没有办法在Java中查看xml? Please provide the method to print the request xml sent. 请提供打印发送的请求xml的方法。

Sample code in java : 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();
       }

}

Setting the following logging parameters should enable the logging of the full wire contents (which should include the XML you're sending) and context. 设置以下日志记录参数应启用对全部线路内容(其中应包括要发送的XML )和上下文的记录。 It's meant to work with the Commons Logging interface. 它旨在与Commons Logging界面一起使用。

-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 -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 =调试

The same level of debug for log4j : log4j进行相同级别的调试:

log4j.rootLogger=INFO, stdout 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] %m%n log4j.appender.stdout = org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout = org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern =%5p [%c]%m%n

log4j.logger.org.apache.http=DEBUG log4j.logger.org.apache.http = DEBUG

More info 更多信息

Edit in response to comment from OP: 根据OP的评论进行编辑:

The link above shows complete examples. 上面的链接显示了完整的示例。 It depends on which logging framework you're using. 这取决于您使用的日志记录框架。 For example, if you're using log4j , a very common choice for logging, you would create a log4j.properties file with the specified log4j config parameters (the second block I specified above). 例如,如果您使用log4j (一种非常常用的日志记录选择),则将使用指定的log4j配置参数(我在上面指定的第二个块)创建一个log4j.properties文件。

Once the log settings take effect, nothing explicitly should need to be printed in your code. 日志设置生效后,无需在代码中明确打印任何内容。 Existing debug logging in the Apache HTTP code should log the responses/requests, among other stuff, assuming the right settings are in place. 假设正确的设置正确,Apache HTTP代码中的现有调试日志记录应该记录响应/请求以及其他内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM