Java:如何发送XML请求?

Val*_*lva 7 java xml request

我需要在java中发送一个xml请求并捕获响应.我怎样才能做到这一点 ?

我在谷歌搜索,但直到现在还没有任何实力.

此致,Valter Henrique.

bdo*_*han 8

如果您要进行HTTP POST,那么您可以在Java SE中使用java.net.* 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);
    }
Run Code Online (Sandbox Code Playgroud)