Question

Je suis en train d'obtenir un InputStream à partir d'une URL. L'URL peut être une ouverture de Firefox. Il retourne un JSON et je l'ai installé un addon pour l'affichage JSON dans Firefox afin que je puisse le voir là-bas.

J'ai donc essayé de l'obtenir de Java par:

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

Mais il jette un IOException dans urlConnection.getInputStream ().

J'ai essayé aussi:

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

Mais pas de chance.

Toute information est appréciable. Merci d'avance.

Était-ce utile?

La solution

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")));

Autres conseils

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");

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

encode the parameters in the URL as follows:

String messageText = URLEncoder.encode(messageText, "UTF-8");
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top