简体   繁体   中英

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. Is there a way to see that xml in java ? Please provide the method to print the request xml sent.

Sample code in 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. It's meant to work with the Commons Logging interface.

-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

The same level of debug for log4j :

log4j.rootLogger=INFO, stdout

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

More info

Edit in response to comment from 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).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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