0

I need to send a xml request from a java standalone class. The xml request that I need to send is of the form -

http://url/query.do?
Request=<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Request initialTime="2011-03-11T16:40Z">
<Query>Java</Query></Request>

So i have put in

String xmlRequest = "url/query.do? Request=<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <Request initialQueryTime="2011-03-11T16:40Z"> <Query>Java</Query></Request>"

and then

URL url = new URL(xmlRequest);
     URLConnection conn = (URLConnection)url.openConnection();
     //conn.connect();
     BufferedReader in = new BufferedReader(
                    new InputStreamReader(
                    conn.getInputStream()));

But I am getting a IOException - Server returned HTTP response code: 505 for URL at sun.net.www.protocol.http.HttpURLConnection.getInputStream.

This url when hit through a browser returns a xml response and I need to parse that XML using STAX.

Can anyone please provide some idea on how to achieve the above.

Can someone please provide some sample code or correct my code so that I am able to form the xml request and parse the xml response. Please help.

Thanks,

swati

mac
  • 42,153
  • 26
  • 121
  • 131
swati
  • 11
  • 1
  • 6
  • The xml request is something http://url/query.do? Request= java – swati Jul 05 '11 at 06:12
  • Please show a short but *complete* program. "Something like this" isn't terribly helpful when the problem may well be in what you're using as the URL. Putting XML in a *URL* seems very strange indeed... it would normally be in a request *body*. – Jon Skeet Jul 05 '11 at 06:12
  • Hi Jon, have edited my code part. see advise now. – swati Jul 05 '11 at 06:36

3 Answers3

1

Symbols '?', '&' and '=' are treated as argument and value separators in URL, so try to encode your XML first.

String xmlRequest = "url/query.do?Request=" + URLEncoder.encode("your xml", "UTF-8");
URL url = new URL(xmlRequest);
...

Browser performs URL encoding for every request. That's why it works.

Dmitry
  • 56
  • 3
0

This question is related to this one.

The error code 505 means the following:

The server does not support, or refuses to support, the HTTP protocol version that was used in the request message. The server is indicating that it is unable or unwilling to complete the request using the same major version as the client.

Community
  • 1
  • 1
Koekiebox
  • 5,793
  • 14
  • 53
  • 88
-2

its advisable to use apache HTTP client library (it has many features which can be used in your case).

you can also use Apache Tika api.

consider the size of the URL being posted refer here..

using the apache HTTP client gets you the option to use the POST method instead of the GET method, this way you can post a fairly large file in the request.

Community
  • 1
  • 1
Anantha Sharma
  • 9,920
  • 4
  • 33
  • 35