Java: Reading from a URL produces gibberish

2019-09-02 04:12发布

So I've been trying to read the html code from kickass.to(it works fine on other sites) but all I get is some weird gibberish. My code:

BufferedReader in = new BufferedReader(
                 new InputStreamReader(new URL("http://kickass.to/").openStream()));
            String s = "";
            while ((s=in.readLine())!=null) System.out.println(s);
            in.close();

For example: enter image description here Does anyone knows why it does that? thanks!

1条回答
别忘想泡老子
2楼-- · 2019-09-02 04:26

The problem here is a server that is probably not configured correctly, as it returns its response gzip compressed, even if the client does not send an Accept-Encoding: gzip header.

So what you're seeing is the compressed version of the page. To decompress it, pass it through a GZIPInputStream:

BufferedReader in = new BufferedReader(
    new InputStreamReader(
         new GZIPInputStream(new URL("http://kickass.to/").openStream())));
查看更多
登录 后发表回答