[英]How to extract the xml sent in a Http Post request through java?
我正在用Java发出http发布请求,我需要查看请求中发送的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();
}
}
设置以下日志记录参数应启用对全部线路内容(其中应包括要发送的XML )和上下文的记录。 它旨在与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 =调试
对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]%m%n
log4j.logger.org.apache.http = DEBUG
根据OP的评论进行编辑:
上面的链接显示了完整的示例。 这取决于您使用的日志记录框架。 例如,如果您使用log4j (一种非常常用的日志记录选择),则将使用指定的log4j配置参数(我在上面指定的第二个块)创建一个log4j.properties文件。
日志设置生效后,无需在代码中明确打印任何内容。 假设正确的设置正确,Apache HTTP代码中的现有调试日志记录应该记录响应/请求以及其他内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.