response expectedContentLength return -1
Asked Answered
C

2

3

Hello i need to create a progressView when i load data from my webservice.

Actually the expectedContentLength alway return -1.

After look lots of similary problem it looks like my webservice never send the Content-Length:.

Then i check with CURL and here is the result :

< HTTP/1.1 200 OK
< Date: Thu, 21 Jun 2012 10:04:39 GMT
< Server: Apache/2.2.16 (Debian)
< X-Powered-By: PHP/5.3.3-7+squeeze9
< cache-control: no-cache
< Vary: Accept-Encoding
< Content-Type: text/html; charset=UTF-8
< Content-Length: 3239  
< Connection: Keep-Alive
< Set-Cookie: PHPSESSID=u4o4i9dofdgnkfmtnf163635j6; path=/

and here is my code to catch length

long long expectDataSize;
long long currentDataSize;

....

 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response
{
    NSLog(@"expect content length %lld", [response expectedContentLength]);
    expectDataSize = [response expectedContentLength];
    currentDataSize = 0;
}

Anyone have already see this problem ?

Cutshall answered 21/6, 2012 at 10:15 Comment(2)
Try to: NSLog(@"%@", [response allHeaderFields]); and see if the response object actually has the Content Length header field.Stambul
code sample here #23585932Rabelais
C
19

Ok i've fix it myself here is my real problem and a solution :

When i ask with Curl i can get the length no problem.

But when i use NSurlConnection

NSURLConnection *c = [[NSURLConnection alloc] initWithRequest:req delegate:self];

The response will be compress with "Gzip" (don't ask me why). And if the response is encode with Gzip it's impossible to know the length, then "expectedContentLength" return -1. And "Content-Length" is absent from [response allHeaderFields].

If you really want to get the length you can simply force to not use Gzip like this :

[req setValue:@"" forHTTPHeaderField:@"Accept-Encoding"];
Cutshall answered 26/6, 2012 at 9:26 Comment(2)
' NSURLResponse ' doesn't have ' allHeaderFields' property. how can I know the expected length ?Bruner
I am using NSHTTPURLResponse, not NSURLResponseCutshall
H
0

I got the same problem it seems like the expected file size is unknown thats why it returns -1.

see similar threat why [response expectedContentLength] always return -1

Haggar answered 23/6, 2012 at 16:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.