200字
response expectedContentLength return -1
2012-12-31
2026-01-26
Dev
When I use NSURLConnection to get file, the expectedContentLength alway return -1. After look lots of similary problem it looks like never send the Content-Length. Checking the code below, you will get a surprise:  replace _ssqUrl to your variable.
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:_ssqUrl]];
    [request setCachePolicy:NSURLRequestUseProtocolCachePolicy];
    [request setTimeoutInterval:5.0];
    [request setHTTPMethod:@"GET"];
    [request setValue:@"" forHTTPHeaderField:@"Accept-Encoding"];
    [request setValue:@"application/x-www-form-urlencoded"forHTTPHeaderField:@"Content-Type"];
Most importent is :
[request setValue:@"" forHTTPHeaderField:@"Accept-Encoding"];
The response will be compress with "Gzip". 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]
response expectedContentLength return -1
作者
Chuck
发表于
2012-12-31
License
CC BY-NC-SA 4.0

评论