Java: How to send an XML request?

I need to send an xml request in java and catch the answer. How can i do this?

I'm looking in google, but so far nothing solid.

Regards, Walter Enrique.

+6
source share
3 answers

If you want to perform HTTP POST, you can use java.net. * API 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); } 
+7
source

XML is a data format. If you are talking about requests / responses, you need to know the protocol.

I assume that the protocol you are using is HTTP (S), and you should do a POST with your XML request, but this is just an educated (?) Hunch.

+3
source

You can use playframework. This is the easiest web infrastructure I have ever used in Java. It looks like rails, but in java. Give it a try.

http://www.playframework.org/

It has a convenient and easy to use groovy based template engine. You can set the request format as described here.

http://www.playframework.org/documentation/1.1/routes

See the documentation for details. You are implementing your first website that can send and receive requests in just a few hours.

0
source

Source: https://habr.com/ru/post/887748/


All Articles