Java:从URL读取会产生乱码 [英] Java: Reading from a URL produces gibberish

查看:283
本文介绍了Java:从URL读取会产生乱码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我一直在尝试从kickass.to读取html代码(在其他网站上也能正常工作),但我得到的只是些古怪的东西。
我的代码:

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: Does anyone knows why it does that? thanks!

推荐答案

这里的问题是服务器配置不正确,因为它返回了响应gzip即使客户端未发送 Accept-Encoding:gzip 标头也是如此。

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.

所以,看到的是页面的压缩版本。要对其进行解压缩,请通过 GZIPInputStream

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

这篇关于Java:从URL读取会产生乱码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆