首页 > 解决方案 > How to get Content-Length from response headers in CN1?

问题描述

I implemented this but the response headers don't include Content-Length, even though I make sure the server replies that bit specifically. I also verified the response outside CN1 and it includes Content-Length. The full list of headers captured in ReadHeaders is (as seen from Android): null,Alt-Svc,Cache-Control,Connection,Content-Type,Date,ETag,Server,Transfer-Encoding,Vary,X-Android-Received-Millis,X-Android-Response-Source,X-Android-Selected-Protocol,X-Android-Sent-Millis,X-Cloud-Trace-Context,X-Powered-By. Right now to estimate download sizes I (1) call endpoint to get total size (2) call endpoint to get actual download and use NetworkManager progress listener, but it would be nice to be able to track progress with only one request (by using Content-Length). The vanilla RequestBuilder doesn't expose response headers so a direct usage of ConnectionRequest with readHeaders is needed. But the Content-Length is missing from getHeaderFieldNames

Note:

标签: codenameone

解决方案


这不起作用的原因是因为默认情况下 Android/CN1 发送带有标头的请求Accept-Enconding: gzip。这将返回一个不包含长度标头的分块响应。我不能保证这种行为与每个服务器响应匹配,但在我的情况下它确实如此(Node.js + Express)

要强制服务器返回非分块响应,请将标头设置为“compress”、“identity”或“deflate”

例子:

Rest.post(url).header("Accept-Encoding", "compress").fetchAsJsonMap(resp -> {...

推荐阅读