我正在尝试使用 Web Of Knowledge(WoK) 中的 API 来获取一些数据。文档说明您必须通过 HTTPS 执行 POST 请求,发送包含查询的 XML。但我只收到错误 400 表单服务器。 (错误请求)
这是我的代码,我在 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();
}
}
最佳答案
您发送的 XML 有问题。您将不得不查看服务器日志以找出确切的内容,因为 400 故意尽可能少地告诉您。
关于java - 在 Java 中使用 XML 发出 HTTPS Post 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7594675/