1

*

注意:关于这个主题有几个链接,但它对我不起作用,所以我必须在这里发布新的链接。

*

基本上,我试图显示 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

我使用了以下链接,但没有一个对我有帮助: Link1Link2Link3

请帮我。谢谢!

更新代码: 在服务器端设置 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"];

在任何一方(服务器/客户端)上是否还有更多工作要做。

4

1 回答 1

1

我不知道如何在服务器端添加内容长度 - 我之前遇到过问题,在一种情况下,我遇到了与你类似的情况 - 通过转储所有标题可以看到内容长度,但 iOS 没有在委托方法中交给我。获得它的标准方法是通过:

- (void)connection:(NSURLConnection *)conn didReceiveResponse:(NSURLResponse *)response
{
  ...
  NSUInteger responseLength = response.expectedContentLength == NSURLResponseUnknownLength ? 1024 : (NSUInteger)response.expectedContentLength;

我实际上输入了一个错误:rdar://15605995“HTTP Get response is 200 but response.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);
}

对于特殊情况,这对我来说真的很难。我正在访问公司的内部数据库,我必须先使用名称/密码,然后在手机上获取一个代码(我稍后输入)才能进入。我可以获取其他信息来帮助您吗?怎么可能header里明明有内容长度,你却看不到还给我?


苹果关闭了我的错误,说“按预期工作”。真的!?!?!?!?!?!?!

[我不知道 NSURLSession 是否修复了它。]

编辑:我应该补充一点,在过去,我连接到 DID 的其他站点会导致正确设置“expectedContentLength”。

于 2015-07-06T18:19:47.083 回答