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();
}
}
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.