You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@olingo.apache.org by Prerna Chawla <pr...@gmail.com> on 2014/05/05 14:59:08 UTC

Getting 400 BadRequest in Java, while url working fine in browser

Hi,

Below is the  sample code

         String url= "
http://services.odata.org/Northwind/Northwind.svc/Customers?
 $select=CustomerID,CompanyName,ContactName,ContactTitle,Address,City,Region,PostalCode,Country,Phone,Fax&$top=100&$skip=0&$filter=City
eq 'Berlin'";

try {
URL urlconn=new URL(url);
HttpURLConnection connect=(HttpURLConnection)urlconn.openConnection();
connect.setRequestMethod("GET");
connect.setRequestProperty(HttpHeaders.ACCEPT,HttpContentType.APPLICATION_ATOM_XML_FEED);
 InputStream content=connect.getInputStream();   //crashing here

}
It is crashing while getting the InputStream giving the following
exception* java.io.IOException:
Server returned HTTP response code: 400 for URL:
http://services.odata.org/Northwind/Northwind.svc/Customers?$select=CustomerID,CompanyName,ContactName,ContactTitle,Address,City,Region,PostalCode,Country,Phone,Fax&$top=100&$skip=0&$filter=City
<http://services.odata.org/Northwind/Northwind.svc/Customers?$select=CustomerID,CompanyName,ContactName,ContactTitle,Address,City,Region,PostalCode,Country,Phone,Fax&$top=100&$skip=0&$filter=City>
eq 'Berlin'*
* at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown
Source)*


However if I put the URL directly in the browser ,I get the results without
any error.Why is this so?
How to correct it?


Thanks
Prerna

Re: Getting 400 BadRequest in Java, while url working fine in browser

Posted by "Klevenz, Stephan" <st...@sap.com>.
Hi Prerna,

400 is 'bad request'. Your code looks good and I can't reproduce. Could it be that you are behind a proxy? Check this [1]. Try also a simpler URL and find out if service connection works in general.

Regards,
Stephan

[1] http://stackoverflow.com/questions/1432961/how-do-i-make-httpurlconnection-use-a-proxy

From: Prerna Chawla <pr...@gmail.com>>
Reply-To: "user@olingo.apache.org<ma...@olingo.apache.org>" <us...@olingo.apache.org>>
Date: Montag, 5. Mai 2014 14:59
To: "user@olingo.apache.org<ma...@olingo.apache.org>" <us...@olingo.apache.org>>
Subject: Getting 400 BadRequest in Java, while url working fine in browser

Hi,

Below is the  sample code

         String url= "http://services.odata.org/Northwind/Northwind.svc/Customers?    $select=CustomerID,CompanyName,ContactName,ContactTitle,Address,City,Region,PostalCode,Country,Phone,Fax&$top=100&$skip=0&$filter=City eq 'Berlin'";

try {
URL urlconn=new URL(url);
HttpURLConnection connect=(HttpURLConnection)urlconn.openConnection();
connect.setRequestMethod("GET");
connect.setRequestProperty(HttpHeaders.ACCEPT,HttpContentType.APPLICATION_ATOM_XML_FEED);
InputStream content=connect.getInputStream();   //crashing here

}
It is crashing while getting the InputStream giving the following exception java.io.IOException: Server returned HTTP response code: 400 for URL: http://services.odata.org/Northwind/Northwind.svc/Customers?$select=CustomerID,CompanyName,ContactName,ContactTitle,Address,City,Region,PostalCode,Country,Phone,Fax&$top=100&$skip=0&$filter=City eq 'Berlin'
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)


However if I put the URL directly in the browser ,I get the results without any error.Why is this so?
How to correct it?


Thanks
Prerna