*
注意:关于这个主题有几个链接,但它对我不起作用,所以我必须在这里发布新的链接。
*
基本上,我试图显示 UIProgressView 以显示从 Web 服务获取的数据。现在的问题是,我从我的应用程序调用的几乎所有服务都获得了 expectedContentLength ,并且响应标头包含Content-Length。但是对于 Web 服务的响应之一,它不会返回预期的内容长度,而是始终返回 -1。
以下是我为两个不同的 Web 服务响应获得的响应标头:
- 我获得内容长度的响应标头。(来自服务的数据并不大)
{
Connection = "Keep-Alive";
"Content-Length" = 2689;
"Content-Type" = "application/json";
Date = "Tue, 23 Jun 2015 08:19:04 GMT";
"Keep-Alive" = "timeout=5, max=98";
Server = "Apache/2.4.10 (Win32) OpenSSL/1.0.1i PHP/5.5.15";
"X-Powered-By" = "PHP/5.5.15";
}
- 我没有得到内容长度的响应头。(响应包含大量数据,没有要下载的文件只是JSON数据)
{
Connection = "Keep-Alive";
"Content-Type" = "application/json";
Date = "Tue, 23 Jun 2015 08:12:34 GMT";
"Keep-Alive" = "timeout=5, max=97";
Server = "Apache/2.4.10 (Win32) OpenSSL/1.0.1i PHP/5.5.15";
"Transfer-Encoding" = Identity;
"X-Powered-By" = "PHP/5.5.15";
}
以上服务响应包含大量数据。当在Advanced rest client chrome 的扩展上测试响应时,它给出了以下响应标头,并带有“Transfer-Encoding”= chunked;。
Date: Tue, 23 Jun 2015 12:01:20 GMT
Server: Apache/2.4.10 (Win32) OpenSSL/1.0.1i PHP/5.5.15
X-Powered-By: PHP/5.5.15
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: application/json
我使用了以下链接,但没有一个对我有帮助: Link1, Link2, Link3
请帮我。谢谢!
更新代码: 在服务器端设置 Content-Lenth 标头后,我在Advanced rest client chrome's extension上获得了 Content-Length 标头的有效响应。
Date: Wed, 24 Jun 2015 05:25:48 GMT
Server: Apache/2.4.10 (Win32) OpenSSL/1.0.1i PHP/5.5.15
X-Powered-By: PHP/5.5.15
Content-Length: 1272347
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: application/json
但是在客户端(即在 iOS 应用程序中),我仍然没有得到 Content-Lenth 标头,因此我可以获得预期的ContentLength 值。我仍然得到“传输编码”=身份;在我这边的响应头中。我还在请求中设置了 Accept-Encoding 标头,如下所示:
[req setValue:@"identity;q=0" forHTTPHeaderField:@"Accept-Encoding"];
在任何一方(服务器/客户端)上是否还有更多工作要做。