Java:如何发送 XML 请求?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5953631/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: How to send a XML request?
提问by Valter Silva
i need to send a xml request in java and catch the response. How can i do this ?
我需要在java中发送一个xml请求并捕获响应。我怎样才能做到这一点 ?
I search in the google but nothing solid until now.
我在谷歌搜索,但直到现在还没有什么可靠的。
Best regards, Valter Henrique.
最好的问候,瓦尔特·恩里克。
回答by bdoughan
If you are looking to do an HTTP POST, then you could use the java.net.*APIs in Java SE:
如果您希望执行 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);
}
回答by Olaf
XML is a data format. If you talk about requests/responses, you need to know the protocol.
XML 是一种数据格式。如果您谈论请求/响应,则需要了解协议。
My guess is that the protocol you are using is HTTP(S) and you have to do a POST with your XML request, but this is just an educated(?) guess.
我的猜测是您使用的协议是 HTTP(S) 并且您必须对您的 XML 请求进行 POST,但这只是一个受过教育的(?)猜测。
回答by Yekmer Simsek
You can use playframework. It is the easiest web framework I have ever used in Java. It is resembles to rails but in java. Give it a try.
您可以使用游戏框架。它是我在 Java 中使用过的最简单的 Web 框架。它类似于rails,但在java中。试一试。
It has a nice and easy to use template engine based on groovy. You can set a request format as described here.
它有一个基于 groovy 的漂亮且易于使用的模板引擎。您可以按照此处所述设置请求格式。
http://www.playframework.org/documentation/1.1/routes
http://www.playframework.org/documentation/1.1/routes
Go for documentation for details. You will implement your first website that can send and get requests in just hours.
有关详细信息,请参阅文档。您将实现您的第一个网站,该网站可以在数小时内发送和接收请求。