简体   繁体   English

OkHttp3 如何从 http header 中检索 Http 内容长度?

[英]OkHttp3 How I can retrieve the Http Content Length from http header?

I try to download a file and check whether has been downloaded as a whole:我尝试下载一个文件并检查是否已整体下载:


Request request = new Request.Builder().url("http://example.com/myfile.tar.gz").build();
Response response = client.newCall(request).execute();

// Status code checks goes here
if (downloadedTar == null) {
  throw new SettingsFailedException();
}
ResponseBody downloadedTar = response.body();
double contentLength = Double.parseDouble(response.header("content-length"));

File file = File.createTempFile(System.currentTimeMillis()+"_file", ".tar.gz", getContext().getCacheDir());
FileOutputStream download = new FileOutputStream(file);

download.write(downloadedTar.body().bytes());
download.flush();
download.close();

if(file.exists() && (double)file.length() == contentLength){
  // file has been downloaded
}

But the line:但是这条线:

double contentLength = Double.parseDouble(response.header("content-length"));

But response.header("content-length") is Null and has no integer value, I also tried the following variation response.header("Content-Length") and response.header("Content-Length") without any success.但是response.header("content-length")是 Null 并且没有 integer 值,我也尝试了以下变体response.header("Content-Length")response.header("Content-Length")没有任何成功。

So why I cannot retrieve Content-Length header and how I can ensure that file has been sucessfully downloaded?那么为什么我无法检索 Content-Length header 以及如何确保该文件已成功下载?

Content-Length is removed in a number of cases such as Gzip responses Content-Length 在许多情况下被删除,例如 Gzip 响应

https://github.com/square/okhttp/blob/3ad1912f783e108b3d0ad2c4a5b1b89b827e4db9/okhttp/src/jvmMain/kotlin/okhttp3/internal/http/BridgeInterceptor.kt#L98 https://github.com/square/okhttp/blob/3ad1912f783e108b3d0ad2c4a5b1b89b827e4db9/okhttp/src/jvmMain/kotlin/okhttp3/internal/http/BridgeInterceptor.kt#L98

But generally isn't guaranteed to be present for streamed responses (chunked of h2).但通常不能保证出现流式响应(h2 分块)。

You should try to avoid requiring content-length as it is guaranteed to be present and may change.您应该尽量避免要求 content-length,因为它保证存在并且可能会更改。 Also you can probably optimise your IO with你也可以优化你的 IO

file.sink().buffer().writeAll(response.body.!.source())

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

相关问题 我可以使用 OkHttp3 客户端从 Amazon S3 Presigned URL 获取 header 详细信息“Content-Length”吗? - Can I get the header detail “Content-Length” from an Amazon S3 Presigned URL using OkHttp3 client? 带有OkHttp3和Retrofit2的HTTP / 2 - HTTP/2 with OkHttp3 and Retrofit2 有没有办法从 okhttp3 的失败中获取原始 http 帖子消息? - Is there a way to get to the original http post message from onfailure of okhttp3? 带有json和okhttp3的HTTP发布请求 - HTTP post request with json and okhttp3 Java OkHttp3只使用Http / 1.1或Http / 2 - Java OkHttp3 only use Http/1.1 OR Http/2 OkHttp3是否通过HTTP转发代理支持HTTP2? - Does OkHttp3 support HTTP2 via a HTTP forward proxy? 如何检查是否已从 okHttp3 客户端创建并成功写入临时文件? - How I can inspect that a temporary file has been created and sucessfuly written from an okHttp3 client? 如何获取使用 OkHttp3 传输的实际字节数? - How can I get the number of actual bytes transferred with OkHttp3? 如何找到页面的内容长度<entity>作为 http header 传递?</entity> - How to find the content-length of Page <Entity> to be passed as the http header? 如何在java的http头中将内容长度设置为long值? - How to set content length as long value in http header in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM