Trying to read from a URL(in Java) produces gibberish on certain occaisions

后端 未结 4 802
清歌不尽
清歌不尽 2021-01-23 07:43

I\'m trying to read from a URL, and then print the result.

BufferedReader in = new BufferedReader(
     new InputStreamReader(new URL(\"http://somesite.com/\").o         


        
相关标签:
4条回答
  • 2021-01-23 07:56

    You're probably running into a character encoding issue.

    There should be an HTTP header like the following in the response:

    Content-Type: text/html; charset=UTF-8
    
    0 讨论(0)
  • 2021-01-23 07:56

    Try using telnet to diagnose what's coming over the wire. It may not be textual data. For example, what happens when yo do this?

    telnet somesite.com 80
    GET / HTTP/1.0
    Host: somesite.com
    

    (two carriage returns required after last line)

    This should allow you to see the headers and content coming in and should give you a better clue as to what's going on.

    0 讨论(0)
  • 2021-01-23 08:00

    I had the same issue until I used HttpURLConnection with setChunkedStreamingMode set.

                HttpURLConnection connection = (HttpURLConnection)serverAddress.openConnection();
                connection.setRequestMethod("GET");
                connection.setDoOutput(true);
                connection.setReadTimeout(2000);
                connection.setChunkedStreamingMode(0);
    
                connection.connect();
    
                BufferedReader rd  = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    
                String line = "";
    
                while ((line = rd.readLine()) != null)
                {
                    sb.append(line);
                }
    
                System.out.println(sb.toString());
    
    0 讨论(0)
  • 2021-01-23 08:12

    So after much searching I found the answer to this. The xml is read as gibberish because it is Gzip compressed. The way to read this is by using the GZIPInputStream. This is because the XML is compressed differently.

    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestProperty("Accept-Encoding", "gzip");
        InputStreamReader in = new InputStreamReader (new GZIPInputStream(connection.getInputStream()));
        String str;            
        while (true) {
     int ch = in.read();
     if (ch==-1) {
        break;
     }
    
    0 讨论(0)
提交回复
热议问题