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

标签 java xml post https request

我正在尝试使用 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/

相关文章:

java - 使用模式匹配从文件中排序,Java

java - Jersey 集合的多个默认值

java - 内存不足错误: Java heap space using XSLT transform

javascript - 如何在javascript中使用MD5传输密码

php - 如何将命令行中的curl文件post转换为PHP cURL

java - 无法将 Int 数组转换为 ASCII

java - 如何在 Eclipse 中有效地调试用 JNI 包装的 C 代码? (安卓开发)

xml - XPath如何动态创建属性的路径

sql - 在查询生成的 xml 上添加命名空间

c - 如何用c语言实现gmail的Oauth2.0