response expectedContentLength return -1

/ 0评 / 1

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]

发表评论

邮箱地址不会被公开。 必填项已用*标注