Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: How to send a XML request?

Tags:

java

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 regards, Valter Henrique.

like image 707
Valter Silva Avatar asked May 10 '11 17:05

Valter Silva


1 Answers

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);
    }
like image 125
bdoughan Avatar answered Oct 12 '22 13:10

bdoughan