Java: How to send a XML request?
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 开发者_如何学Cregards, Valter Henrique.
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);
}
XML is a data format. If you talk about requests/responses, you need to know the protocol.
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.
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.
http://www.playframework.org/
It has a nice and easy to use template engine based on groovy. You can set a request format as described here.
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.
精彩评论