codenameone - 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:
解决方案
这不起作用的原因是因为默认情况下 Android/CN1 发送带有标头的请求Accept-Enconding
: gzip
。这将返回一个不包含长度标头的分块响应。我不能保证这种行为与每个服务器响应匹配,但在我的情况下它确实如此(Node.js + Express)
要强制服务器返回非分块响应,请将标头设置为“compress”、“identity”或“deflate”
例子:
Rest.post(url).header("Accept-Encoding", "compress").fetchAsJsonMap(resp -> {...
推荐阅读
- laravel - Laravel 给出错误“您的锁定文件不包含兼容的软件包集。请运行作曲家更新”
- c++ - 使用 Fstream 序列化无序映射
- java - 即使在 VM 选项中指定路径后,也无法加载 sqljdbc_auth.dll
- ruby - RVM 安装 ruby 2.6.4 失败并出现 makefile 错误:函数“ffi_prep_closure”的隐式声明在 C99 中无效
- xml - xml 标签是否允许多个属性?
- sql - 排序规则/空白/空白字符的 SQL 问题
- apache-spark - Databricks:检测到不兼容的格式(临时视图)
- apache-spark - Apache Spark:作业在收集阶段失败,为什么失败在集群端?
- .net - 在 nuget 包生成中进行版本控制
- deployment - 工件可以包含 .jar 文件的列表吗?