*
Note: There are several links on this topic but it didn't work for me, So I have to post the new one here.
*
基本上,我试图显示 UIProgressView 以显示从 Web 服务获取的数据。现在的问题是,我从我的应用程序调用的几乎所有服务都获得了 expectedContentLength,并且响应 header 包含 Content-Length。但是对于 Web 服务的响应之一,它不会返回 expectedContentLength,而是始终返回 -1。
以下是我为两个不同的 Web 服务响应获得的响应 header :
- 我获取内容长度的响应 header 。(来自服务的数据并不大)
{
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";
}
- 我没有得到内容长度的响应 header 。 (响应包含大量数据,没有要下载的文件只是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; 的响应 header 。
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 header 后,我在 Advanced rest client chrome's extension 上得到了带有 Content-Length header 的有效响应。
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 header ,因此我可以获得 expectedContentLength 值。 我仍然得到 "Transfer-Encoding"= Identity;在我这边的响应标题中。 我还在请求中设置了 Accept-Encoding header ,如下所示:
[req setValue:@"identity;q=0" forHTTPHeaderField:@"Accept-Encoding"];
两边(服务器/客户端)还有什么要做的吗?
最佳答案
我不知道如何在服务器端添加内容长度 - 我之前遇到过这个问题,在一种情况下我遇到了与你类似的情况 - 通过转储所有 header 可以看到内容长度,但是iOS 并没有在委托(delegate)方法中交给我。获取它的标准方法是通过:
- (void)connection:(NSURLConnection *)conn didReceiveResponse:(NSURLResponse *)response
{
...
NSUInteger responseLength = response.expectedContentLength == NSURLResponseUnknownLength ? 1024 : (NSUInteger)response.expectedContentLength;
我实际上在这方面输入了一个错误:rdar://15605995“HTTP 获取响应为 200 但 response.expectedContentLength = -1 即使在 header 中找到 Content-Length”
2014-01-08 18:49:36.966 DB_Lookup[36824:3f07] YIKES:LEN=22162 {
"Cache-Control" = "must-revalidate";
Connection = "keep-alive";
"Content-Encoding" = gzip;
"Content-Length" = 22162;
"Content-Type" = "text/html; charset=UTF-8";
Date = "Wed, 08 Jan 2014 23:49:09 GMT";
Expires = 0;
"Front-End-Https" = on;
Pragma = "no-cache";
Server = "nginx/1.2.4";
"Strict-Transport-Security" = "max-age=2592000";
Vary = "Accept-Encoding";
"X-Content-Type-Options" = nosniff;
"X-Frame-Options" = SAMEORIGIN;
"X-Powered-By" = <Company>;
"X-<Company>-Server" = "<private>";
"X-XSS-Protection" = "1; mode=block";
对于这段代码:
if(response.expectedContentLength == NSURLResponseUnknownLength) {
id len = httpResponse.allHeaderFields[@"Content-Length"];
if(len) {
responseLength = [len integerValue];
}
NSLog(@"YIKES:LEN=%@ %@", len, httpResponse.allHeaderFields);
}
这对我来说真的很难处理特殊情况。我正在访问公司内部数据库,我必须先使用名称/密码,然后将密码输入我的手机(我稍后输入)才能进入。是否有其他信息可以帮助你?怎么可能header里明明有content length,你却看不到还给我?
Apple 关闭了我的错误,说“按预期工作”。真的!?!?!?!?!?!?!
[我不知道 NSURLSession 是否修复了它。]
编辑:我应该补充说,在过去,我连接到 DID 的其他网站会导致正确设置“expectedContentLength”。
关于ios - NSURLConnection 响应的 expectedContentLength -1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31002572/