*
注意:此主题有几个链接,但它对我不起作用, 所以我必须在这里发布新的。
*
基本上我正在尝试显示UIProgressView以显示从Web服务获取的数据。现在的问题是,我从我的应用程序调用的几乎所有服务都获得了expectedContentLength,响应头包含 Content-Length 。但对于其中一个Web服务的响应,它不会返回expectedContentLength,而是始终返回-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";
}
Above服务响应包含大量数据。在高级休息客户端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标头后,我在高级休息客户端chrome扩展上获得了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标头,因此我可以获得expectedContentLength值。 我仍然得到“转移编码”=身份;在我这边的回复标题中。 我还在请求中设置了Accept-Encoding标头,如下所示:
[req setValue:@“identity; q = 0”forHTTPHeaderField:@“Accept-Encoding”];
在任何一方(服务器/客户端)还有什么要做的。
答案 0 :(得分:1)
我不知道如何在服务器端添加内容长度 - 我以前遇到过这个问题,在一种情况下我遇到了与你类似的情况 - 通过转储所有标题可以看到内容长度,但是iOS没有在委托方法中将它交给我。获得它的标准方法是通过:
- (void)connection:(NSURLConnection *)conn didReceiveResponse:(NSURLResponse *)response
{
...
NSUInteger responseLength = response.expectedContentLength == NSURLResponseUnknownLength ? 1024 : (NSUInteger)response.expectedContentLength;
我实际上输入了一个错误:rdar:// 15605995“HTTP Get响应是200但是响应.expectedContentLength = -1,即使在标题中找到了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);
}
对于特殊情况,这对我来说真的很难。我正在访问内部公司数据库,我必须首先使用名称/密码,然后获取一个代码到我的手机(我稍后输入)进入。是否有其他信息我可以帮助你?如何在标题中清楚地显示内容长度,但是你没有看到并将它返回给我?
Apple关闭了我的错误,说“按预期工作”。真的!?!?!?!?!?!?![我不知道NSURLSession是否修好了。]
编辑:我应该补充说,过去,我连接到DID的其他网站会导致正确设置'expectedContentLength'。