Sending XML as the body of a HTTP request

I have to write an application that interacts with a server that expects XML encoded commands as the body of a HTML request. Can anyone tell me how I should do this.Thanks.
[186 byte] By [smcelroy1a] at [2007-10-2 17:08:28]
# 1
XML documents are just text. HTTP requests are just text. So what's your question? Make the XML document, put it in the request. If you don't know how to do either or both of those things then ask a more specific question. (Much more specific if possible.)
DrClapa at 2007-7-13 18:23:14 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2
Thanks for your help. You manners are impeccable.
smcelroy1a at 2007-7-13 18:23:14 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3
You're welcome. And if you do have a specific question, don't hesitate to ask it. I see from one of your other posts that you do know something about XML, but you sure couldn't tell that from this one.
DrClapa at 2007-7-13 18:23:14 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4

OK. Here goes.

I have to write a standalone Java application that communicates with a server by sending XML requests in the body of a HTML document.

Ther server returns XML.

I am creating my XML using castor and I have looked at using HttpURLConnection to connect to the server.

In the code below the LoginRequestHelp uses castor to generate the XML required for logging in to the system.

The problem with this coode is that it sends the request to the server using a URL parameter not as the body of the HTML request

public void login()

{

LoginRequestHelper lrh = new LoginRequestHelper();

lrh.setPassword(getPassword());

lrh.setUserName(getUserName());

try

{

URL url = new URL(<some url>);

HttpURLConnection c = (HttpURLConnection)url.openConnection();

c.setDoOutput(true);

c.setDoInput(true);

c.setUseCaches(false);

String xml = lrh.getCommandListXML();

String data = URLEncoder.encode("xmlrequest", "UTF-8") + "=" + URLEncoder.encode(xml, "UTF-8");

c.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

OutputStreamWriter wr = new OutputStreamWriter(c.getOutputStream());

System.out.println("sending...\n" + data);

wr.write(data);

wr.flush();

System.out.println("Recieved... ");

// Get the response

BufferedReader rd = new BufferedReader(new InputStreamReader(c.getInputStream()));

String line;

while ((line = rd.readLine()) != null)

{

System.out.println(line);

}

wr.close();

rd.close();

System.out.println(lrh.getCommandListXML());

}

catch (MalformedURLException e)

{

// TODO Auto-generated catch block

e.printStackTrace();

}

catch (IOException e)

{

// TODO Auto-generated catch block

e.printStackTrace();

}

}

smcelroy1a at 2007-7-13 18:23:14 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 5

Okay, I don't know much about HTTP requests (this is the XML forum after all), so maybe this answer is hopelessly naive. But it looks to me like you're sending "xmlrequest=(yourxmldata)" as the request, and that looks like a URL parameter to me as well. What happens if you just send the XML data without "xmlrequest=" before it? I'm assuming your content type is correct and all the rest of it as well.

DrClapa at 2007-7-13 18:23:14 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 6
Sending the request without the paramter name causes an error. Thanks anyways.
smcelroy1a at 2007-7-13 18:23:14 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 7
Maybe this helps : c.setRequestMethod("POST"); // default is a GET request !
Ollebola at 2007-7-13 18:23:14 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...