Server returned HTTP response code: 400

前端 未结 5 1480
一个人的身影
一个人的身影 2020-12-10 12:37

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

5条回答
  •  误落风尘
    2020-12-10 13:30

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

提交回复
热议问题