7

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.

Valter Silva
  • 16,116
  • 50
  • 134
  • 213

3 Answers3

8

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);
    }
bdoughan
  • 145,155
  • 22
  • 291
  • 386
3

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.

Olaf
  • 6,173
  • 1
  • 18
  • 37
0

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.

Yekmer Simsek
  • 4,052
  • 3
  • 20
  • 19