Java:如何发送 XML 请求? [英] Java: How to send a XML request?

查看:24
本文介绍了Java:如何发送 XML 请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i need to send a xml request in java and catch the response. How can i do this ?

我在谷歌搜索,但直到现在都没有可靠的.

I search in the google but nothing solid until now.

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

Best regards, Valter Henrique.

推荐答案

如果您希望进行 HTTP POST,那么您可以使用 java.net.* Java SE 中的 API:

If you are looking to do an HTTP POST, then you could use the java.net.* APIs in Java SE:

    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 请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆