Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Server returned HTTP response code: 400

Tags:

java

I am reading data from a webservice. The issue if I put the link on the browser it works fine. When I run like this give me error. I am suspecting is it due to the way how I send my parameters. My paramater list has this dID=1,5,7,11,14,18,26&FromDate=18 Sep 2012 00:00 am&ToDate=18 Sep 2012 10:00 am. Do I need to do some encoding here?

URL xmlURLDM = new URL(urlDM);
InputStream xml2 = xmlURLDM.openStream();

I get this error

java.io.IOException: Server returned HTTP response code: 400 for URL: 
 at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1612)
        at java.net.URL.openStream(URL.java:1035)
        at xmlreader.main(xmlreader.java:172)
like image 276
user837306 Avatar asked Sep 14 '25 05:09

user837306


2 Answers

You do need encoding, most likley it is the spaces in your URL that is causing the trouble. Use Javas built in url-encoding. eg:

String encoded = URLEncoder.encode(myUrl, "UTF-8");

... call web service with encoded as URL

There can be other reasons for the status code being 400, but this encoding issue is probably your first stumbling block.

like image 187
Paul Jowett Avatar answered Sep 16 '25 20:09

Paul Jowett


The Documentation of URL says,

The URL class does not itself encode or decode any URL components according to the escaping mechanism defined in RFC2396. It is the responsibility of the caller to encode any fields, which need to be escaped prior to calling URL, and also to decode any escaped fields, that are returned from URL. Furthermore, because URL has no knowledge of URL escaping, it does not recognise equivalence between the encoded or decoded form of the same URL.

So please use URLEncoder.encode() before you invoke URL()

like image 32
Santosh Avatar answered Sep 16 '25 20:09

Santosh