Java:如何发送 XML 请求?

标签 java xml request

我需要在 java 中发送一个 xml 请求并捕获响应。 我该怎么做?

我在谷歌上搜索,但直到现在都没有找到可靠的东西。

最好的问候, 瓦尔特·恩里克。

最佳答案

如果你想做一个 HTTP POST,那么你可以使用 java.net.* Java SE 中的 API:

    try { 
        URL url = new URL(URI);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setInstanceFollowRedirects(false);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/xml");

        OutputStream os = connection.getOutputStream();
        // Write your XML to the OutputStream (JAXB is used in this example)
        jaxbContext.createMarshaller().marshal(customer, os);
        os.flush();
        connection.getResponseCode();
        connection.disconnect();
    } catch(Exception e) {
        throw new RuntimeException(e);
    }

关于Java:如何发送 XML 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5953631/

相关文章:

php - 寻找一种使用 php 解析 XML 字符串并将其插入 MySQL 的方法

asp.net - 解决大型 RewriteMap 配置部分

html - Go r.FormValue 为空

java - 为什么在 java 中使用 request.getParameter() 时字符损坏?

java - 如何将字符串日期解析为时间戳或日期

java - 从通用数组列表中查找最大值

java - 如何在Java中转义正则表达式的文本?

java - 在 JEditorPane 中显示带有样式表的 XML

json - postman - 如何将全局变量传递到 JSON 正文

java - Selenium 显示错误 "The method sendKeys(CharSequence[]) in the type WebElement is not applicable for the arguments (String)"