Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Server returned HTTP response code: 400

I am trying to get an InputStream from a URL. The URL can be a opened from Firefox. It returns a json and I have installed an addon for viewing json in Firefox so I can view it there.

So I tried to get it from Java by:

URL url = new URL(urlString);
URLConnection urlConnection = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));

But it is throwing an IOException in urlConnection.getInputStream().

I also tried:

HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = url.openStream();

But no luck.

Any information is appreciable. Thanks in advance.

like image 344
Tapas Bose Avatar asked Feb 19 '12 13:02

Tapas Bose


People also ask

What does HTTP response code 400 refers to?

The HyperText Transfer Protocol (HTTP) 400 Bad Request response status code indicates that the server cannot or will not process the request due to something that is perceived to be a client error (for example, malformed request syntax, invalid request message framing, or deceptive request routing).

Why do I keep getting 400 Bad Request on Chrome?

What causes bad request errors on Chrome? Error 400 is a client error that occurs due to incorrect requests, invalid syntax, or routing issues. It can also occur if the URL is not recognized or you did not type it correctly. So, check again and make sure you typed the URL correctly.

Why do I keep getting bad request error?

The most common reason for a 400 Bad Request error is because the URL was typed wrong or the link that was clicked on points to a malformed URL with a specific kind of mistake in it, like a syntax problem. This is most likely the problem if you get a 400 Bad Request error.


3 Answers

Thank you everybody. This is a weird problem but at last I solved it.

The URL I am requesting is

http://api.themoviedb.org/2.1/Movie.search/en/json/api_key/a nightmare on elm street 

Now browser replaces the spaces between "a nightmare on elm street" by "%20" internally and parses. That is why the requested server can response by that request. But From Java I didn't replaced that spaces by "%20", so it turns into Bad Request, source.

Now it is working.

BufferedReader reader = new BufferedReader(new InputStreamReader(((HttpURLConnection) (new URL(urlString)).openConnection()).getInputStream(), Charset.forName("UTF-8")));
like image 138
Tapas Bose Avatar answered Oct 18 '22 02:10

Tapas Bose


I had a similar issue and my url was:

http://www.itmat.upenn.edu/assets/user-content/documents/ITMAT17. October 10 2017_.pdf

which obviously contained spaces.

These caused java.io.IOException Server returned HTTP response code: 400 in the following code:

java.net.URL url = new URL(urlString);  
java.io.InputStream in = url.openStream();

If you copy the above url and paste in browser, you will realize that browser adds '%20' for the spaces. So I did it manually with the following code and the problem is solved.

if(urlString.contains(" "))
    urlString = urlString.replace(" ", "%20");

Complete code/answer should be:

if(urlString.contains(" "))
    urlString = urlString.replace(" ", "%20");
java.net.URL url = new URL(urlString);  
java.io.InputStream in = url.openStream();
like image 8
harshainfo Avatar answered Oct 18 '22 03:10

harshainfo


are you setting up the connection correctly? here's some code that illustrates how to do this. Note that I am being lazy about exception handling here, this is not production quality code.

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;


public class URLFetcher {

    public static void main(String[] args) throws Exception {
        URL myURL = new URL("http://www.paulsanwald.com/");
        HttpURLConnection connection = (HttpURLConnection) myURL.openConnection();
        connection.setRequestMethod("GET");
        connection.setDoOutput(true);
        connection.connect();
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        StringBuilder results = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            results.append(line);
        }

        connection.disconnect();
        System.out.println(results.toString());
    }
}
like image 3
Paul Sanwald Avatar answered Oct 18 '22 04:10

Paul Sanwald