如何使用 Java 以正确的编码检索 HTML 页面?

发布于 2024-08-02 01:32:26 字数 290 浏览 3 评论 0原文

如何使用页面编码中的 HTML 页面读取 HTTP 流?

这是我用来获取 HTTP 流的代码片段。 InputStreamReader 有编码可选参数,但我不知道如何获取它。

URLConnection conn = url.openConnection();
InputStream is = conn.getInputStream();
BufferedReader d = new BufferedReader(new InputStreamReader(is));

How can I read HTTP stream with HTML page in page's encoding?

Here is a code fragment I use to get the HTTP stream. InputStreamReader has the encoding optional argument, but I have no ideas about the way to obtain it.

URLConnection conn = url.openConnection();
InputStream is = conn.getInputStream();
BufferedReader d = new BufferedReader(new InputStreamReader(is));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

山田美奈子 2024-08-09 01:32:26

检索网页是一个相当复杂的过程。 这就是 HttpClient 等库存在的原因。 我的建议是,除非您有真正令人信服的理由,否则请使用 HttpClient。

Retrieving a Webpage is a reasonably complicated process. That's why libraries such as HttpClient exist. My advice is that unless you have a really compelling reason otherwise, use HttpClient.

晨敛清荷 2024-08-09 01:32:26

当通过 URLConnection 建立连接时

conn = url.openConnection();

您可以通过 url.getContentEncoding() 获取编码方法名称,因此将此 String 传递给 InputStreamReader() ,因此代码看起来像

BufferedReader d = new BufferedReader(new InputStreamReader(is,url.getContentEncoding()));

When the connection is establised thru

URLConnection conn = url.openConnection();

you can get the encoding method name thru url.getContentEncoding() so pass this String to InputStreamReader() so the code looks like

BufferedReader d = new BufferedReader(new InputStreamReader(is,url.getContentEncoding()));

冷了相思 2024-08-09 01:32:26

简短的答案是 URLConnection.getContentEncoding()。 正确的答案是 cletus 建议的,使用适当的第三方库,除非你有令人信服的理由不这样做。

The short answer is URLConnection.getContentEncoding(). The right answer is what cletus suggests, use an appropriate third party library unless you have a compelling reason not to.

晚风撩人 2024-08-09 01:32:26

我最近有一个非常类似的问题需要解决。 与其他答案一样,我也开始尝试 HttpClient 等。 但是,这些库要求您预先知道要下载的文件的编码。 否则,检索到的 HTML 文件的转换将产生不可读的字符。

这种方法行不通,因为 HTML 文件的编码仅在 HTML 文件本身中指定。 根据 HTML 版本,编码以多种不同的方式指定,例如 XML 标头、两个不同的标头元标记元素等。如果您采用这种方法,您将需要:

  1. 下载文件并查看内容以找出编码通过解析 HTML 内容。
  2. 再次下载文件以指定正确的编码。

特别是解析 HTML 内容以获取正确的编码字符串很容易出错。 相反,我建议您依赖像 JSoup 这样的库,它可以为您完成这项工作。 因此,不要通过 httpclient 下载文件,而是使用 JSoup 为您检索文件。 此外,JSoup 提供了一个很好的 API 来直接访问 HTML 页面的不同部分(例如页面标题)。

I had a very similar problem to solve recently. Like the other answers, I also started playing around with HttpClient et al. However, those libraries require that you know upfront the encoding of the file you want to download. Otherwise, conversion of the retrieved HTML file will yield in unreadable characters.

This approach won't work, because the encoding of the HTML file is specified only in the HTML file itself. Depending on the HTML version, the encoding is specified in many different ways like XML header, two different head meta tag elements, etc. If you follow this approach, you would need to:

  1. Download file and look at the content to figure out the encoding by parsing the HTML content.
  2. Download file a second time to specify proper encoding.

Especially parsing HTML content for proper encoding strings is error-prone. Instead, I suggest you rely on a library like JSoup, which will do the job for you. So instead of downloading the file via httpclient, use JSoup to retrieve the file for you. In addition, JSoup provides a nice API to access different parts of the HTML page directly (e.g. page title).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文