简体   繁体   English

okhttp内容长度为-1,且带有大文件

[英]okhttp content-length is -1 with big files

I am downloading a file with okhttp and things work fine - now I want to show the progress and hit a road-bump. 我正在使用okhttp下载文件,并且一切正常-现在,我想显示进度并遇到麻烦。 The returned content-length is -1. 返回的content-length为-1。 It comes back correctly from the server: 它从服务器正确返回:

⋊> ~ curl -i http://ipfs.io/ipfs/QmRMHb4Vhv8LtYqw8RkDgkdZYxJHfrfFeQaHbNUqJYmdF2                                        13:38:11
HTTP/1.1 200 OK
Date: Tue, 14 Jun 2016 11:38:16 GMT
Content-Type: application/octet-stream
Content-Length: 27865948

I traced the problem down to OkHeaders.java here: 我在这里将问题追溯到OkHeaders.java:

 public static long contentLength(Headers headers) {
    return stringToLong(headers.get("Content-Length"));
 }

I see all the other headers here in headers - but not Content-Length - so headers.get("Content-Length") returns null. 我在标头中看到了所有其他标头,但没有看到Content-Length,因此headers.get(“ Content-Length”)返回null。 Anyone has a clue how this can get lost? 任何人都有线索怎么会迷路?

Interestingly if I change the url to " http://google.com " I get a content-length from okhttp - but with curl both look same Content-Length wise - this really confuses me 有趣的是,如果我将URL更改为“ http://google.com ”,则会从okhttp获得一个内容长度-但是使用curl时,它们在Content-Length上看起来都是相同的-这确实使我感到困惑

Update: it seems to correlate with he size of the file. 更新:似乎与文件大小相关。 If I use smaller content from the same server I get a Content-Length with okhttp. 如果我从同一台服务器使用较小的内容,则可以使用okhttp获得Content-Length。 The problem only happens when the file is big 仅当文件很大时才会出现问题

It looks like above a certain size the server uses chunked encoding and you won't get a content length. 看起来超过一定大小,服务器使用分块编码 ,您将不会获得内容长度。

HTTP/1.1 200 OK
Date: Tue, 14 Jun 2016 14:30:07 GMT
Content-Type: application/octet-stream
Transfer-Encoding: chunked

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM