我需要在 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/