我試圖使用Web of Knowledge(WoK)中的API來獲取一些數據。該文檔解釋說您必須通過HTTPS執行POST請求,發送包含查詢的XML。但我只能得到錯誤400表單服務器。 (Bad Request)在Java中使用XML進行HTTPS Post請求
這是我的代碼,我在Google找到它,並且爲我的情況做了一些修復。
public static void main(String[] args) throws Exception {
// Get target URL
String strURL = /*Here the Server URL*/;
// Get file to be posted
String strXMLFilename = "src/main/resources/xml/wosdata.xml";
File input = new File(strXMLFilename);
// Prepare HTTP post
PostMethod post = new PostMethod(strURL);
// Request content will be retrieved directly
// from the input stream
// Per default, the request content needs to be buffered
// in order to determine its length.
// Request body buffering can be avoided when
// content length is explicitly specified
post.setRequestEntity(new InputStreamRequestEntity(
new FileInputStream(input), input.length()));
// Specify content type and encoding
// If content encoding is not explicitly specified
// ISO-8859-1 is assumed
post.setRequestHeader(
"Content-type", "text/xml; charset=ISO-8859-1");
// Get HTTP client
HttpClient httpclient = new HttpClient();
// Execute request
try {
int result = httpclient.executeMethod(post);
// Display status code
System.out.println("Response status code: " + result);
// Display response
System.out.println("Response body: ");
System.out.println(post.getResponseBodyAsString());
}catch (Exception e) {
e.printStackTrace();
} finally {
// Release current connection to the connection pool
// once you are done
post.releaseConnection();
}
}
在此之前,您需要將從目標網站下載的https證書添加到您的cacerts文件嗎?我想你會如果你使用HttpsURLConnection。 HttpClient可能會爲你處理,但值得檢查。 – mikey
你可以按照[這個](http://hc.apache.org/httpcomponents-client-ga/logging.html)開啓網絡日誌記錄,看看通過電報發送了什麼?通常會增加httpclient的日誌級別。 – beny23
@Mikey如果他得到400,那麼他的HTTPS/SSL必須完美工作,因爲400會返回到HTTP標頭中。 – EJP