【问题标题】:Jsoup and gzipped html content (Android)Jsoup 和 gzipped html 内容 (Android)
【发布时间】:2011-07-20 22:05:47
【问题描述】:

我整天都在努力让这个东西工作,但它仍然不正确。我在这里查看了很多帖子,并测试了很多不同的实现,我现在不知道在哪里看......

这是我的情况,我的服务器上有一个小的 php 测试文件 (gz.php),看起来像这样:

header("Content-Encoding: gzip");
print("\x1f\x8b\x08\x00\x00\x00\x00\x00");
$contents = gzcompress("Is it working?", 9);
print($contents);

这是我能做到的最简单的方法,它适用于任何网络浏览器。

现在我有一个使用具有此代码的 Jsoup 的 Android 活动:

URL url = new URL("http://myServerAdress.com/gz.php");
doc = Jsoup.parse(url, 1000);

这会导致“Jsoup.parse”行出现空 EOFException。

我到处都读到 Jsoup 应该解析 gzip 压缩的内容而不必做任何特别的事情,但很明显,缺少一些东西。

我尝试了许多其他方法,例如使用 Jsoup.connect().get() 或 InpuStream、GZipInputStream 和 DataInpuStream。我也尝试过 PHP 中的 gzDeflate() 和 gzencode() 方法,但也没有运气。我什至尝试不在 PHP 中声明 header-encoding 并尝试稍后对内容进行压缩……但它既聪明又有效……

这一定是我想念的“愚蠢”的东西,但我就是不知道是什么……有人有想法吗?

(ps:我用的是Jsoup 1.7.0,所以目前是最新的)

【问题讨论】:

  • 你能把 PHP 脚本放在某个地方,以便我可以测试它吗?另外,1.6.1 是最新版本。
  • 你是正确的 Jsoup 版本...我很抱歉(我知道我有最新的并且之前的版本是 1.6.0...)。
  • 我今天早上在这个页面上找到了解决方案:gzcompress manual。看起来是页面上自动写入的 crc 有问题。通常的浏览器可以接受,但 Jsoup 不行。所以我用了一种方法来压制它并“瞧”。问题解决了!感谢您的努力;)
  • 酷。 jsoup 只是uses 一个普通的Java GZIPInputStream 来解析gzip,所以你会在任何Java 程序中遇到这个问题。用您找到的答案更新您的问题并将其标记为已回答可能是一个想法。

标签: android gzip jsoup


【解决方案1】:

根据here 的信息,提问者在评论中指出 gzcompress 正在编写一个不正确且不完整的 CRC,操作代码为:

// Display the header of the gzip file
// Thanks ck@medienkombinat.de!
// Only display this once
echo "\x1f\x8b\x08\x00\x00\x00\x00\x00";

// Figure out the size and CRC of the original for later
$Size = strlen($contents);
$Crc = crc32($contents);

// Compress the data
$contents = gzcompress($contents, 9);

// We can't just output it here, since the CRC is messed up.
// If I try to "echo $contents" at this point, the compressed
// data is sent, but not completely.  There are four bytes at
// the end that are a CRC.  Three are sent.  The last one is
// left in limbo.  Also, if we "echo $contents", then the next
// byte we echo will not be sent to the client.  I am not sure
// if this is a bug in 4.0.2 or not, but the best way to avoid
// this is to put the correct CRC at the end of the compressed
// data.  (The one generated by gzcompress looks WAY wrong.)
// This will stop Opera from crashing, gunzip will work, and
// other browsers won't keep loading indefinately.
//
// Strip off the old CRC (it's there, but it won't be displayed
// all the way -- very odd)
$contents = substr($contents, 0, strlen($contents) - 4);

// Show only the compressed data
echo $contents;

// Output the CRC, then the size of the original
gzip_PrintFourChars($Crc);
gzip_PrintFourChars($Size);

Jonathan Hedley 评论道,“jsoup 只需 uses a normal Java GZIPInputStream 即可解析 gzip,因此任何 Java 程序都会遇到这个问题。” EOFException 可能是由于 CRC 不完整。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-04
    • 1970-01-01
    • 1970-01-01
    • 2017-10-16
    • 1970-01-01
    • 2013-11-09
    • 2012-01-03
    相关资源
    最近更新 更多