問題描述
您好,當(dāng)我從 Web 服務(wù)加載數(shù)據(jù)時(shí),我需要?jiǎng)?chuàng)建一個(gè) progressView.
Hello i need to create a progressView when i load data from my webservice.
實(shí)際上,expectedContentLength 總是返回 -1.
Actually the expectedContentLength alway return -1.
看了很多類似的問題后,看起來我的網(wǎng)絡(luò)服務(wù)從未發(fā)送過 Content-Length:.
After look lots of similary problem it looks like my webservice never send the Content-Length:.
然后我用 CURL 檢查,結(jié)果如下:
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=/
這是我捕捉長(zhǎng)度的代碼
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;
}
有人已經(jīng)看到這個(gè)問題了嗎?
Anyone have already see this problem ?
推薦答案
好的,我自己解決了,這是我真正的問題和解決方案:
Ok i've fix it myself here is my real problem and a solution :
當(dāng)我用 Curl 詢問時(shí),我可以得到長(zhǎng)度沒問題.
When i ask with Curl i can get the length no problem.
但是當(dāng)我使用 NSurlConnection 時(shí)
But when i use NSurlConnection
NSURLConnection *c = [[NSURLConnection alloc] initWithRequest:req delegate:self];
響應(yīng)將用Gzip"壓縮(不要問我為什么).如果響應(yīng)是用 Gzip 編碼的,則不可能知道長(zhǎng)度,然后expectedContentLength"返回-1.[response allHeaderFields] 中沒有Content-Length".
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].
如果你真的想獲得長(zhǎng)度,你可以簡(jiǎn)單地強(qiáng)制不要像這樣使用 Gzip:
If you really want to get the length you can simply force to not use Gzip like this :
[req setValue:@"" forHTTPHeaderField:@"Accept-Encoding"];
這篇關(guān)于響應(yīng)預(yù)期內(nèi)容長(zhǎng)度返回 -1的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!