[英]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.