在 Java 中使用 XML 发出 HTTPS Post 请求

发布于 2024-12-07 01:14:13 字数 1726 浏览 0 评论 0原文

我正在尝试使用 Web Of Knowledge(WoK) 的 API 来获取一些数据。文档解释说您必须通过 HTTPS 执行 POST 请求,发送包含查询的 XML。但我只收到错误 400 表单服务器。 (错误的请求)

这是我的代码,我在谷歌中找到了它,我对我的情况做了一些修复。

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();
    }
}

I'm trying to use the API from Web Of Knowledge(WoK) to obtain some data. The documentation explain that you have to do POST Requests through HTTPS, sending a XML which contains the queries. But I only get the error 400 form server. (Bad Request)

Here is my code, I found it in Google and I make some fixes for my case.

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();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

流年里的时光 2024-12-14 01:14:13

您发送的 XML 有问题。您必须查看服务器日志才能准确了解内容,因为 400 故意告诉您尽可能少的信息。

There is something wrong with the XML you are sending. You will have to look at server logs to find out exactly what, as 400 deliberately tells you as little as possible.

空城旧梦 2024-12-14 01:14:13

你应该这样做。首先将 xml 的内容读取到 String 并使用 StringRequestEntity 进行发布。

// Get file to be posted
String strXMLFilename = "src/main/resources/xml/wosdata.xml";
StringBuilder contents = new StringBuilder();

try {
  BufferedReader input =  new BufferedReader(new FileReader(new File(strXMLFilename)));
  try {
    while (( line = input.readLine()) != null){
      contents.append(line);
      contents.append(System.getProperty("line.separator"));
    }
  }
  finally {
    input.close();
  }



  StringEntity requestEntity = new StringEntity(contents.toString());
  post.setEntity(requestEntity);

You should do it like this. First read the contents of the xml to String and do post using a StringRequestEntity.

// Get file to be posted
String strXMLFilename = "src/main/resources/xml/wosdata.xml";
StringBuilder contents = new StringBuilder();

try {
  BufferedReader input =  new BufferedReader(new FileReader(new File(strXMLFilename)));
  try {
    while (( line = input.readLine()) != null){
      contents.append(line);
      contents.append(System.getProperty("line.separator"));
    }
  }
  finally {
    input.close();
  }



  StringEntity requestEntity = new StringEntity(contents.toString());
  post.setEntity(requestEntity);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文