0

デリゲートの使用

- (void)connection: (NSURLConnection*) connection didReceiveResponse: (NSHTTPURLResponse*) response {

ダウンロード サイズは、次のように long long として取得できます。

downloadSize = [response expectedContentLength];

しかし、私のdownloadSizeは時々-1です。正しい値の場合もありますが、半分は -1 を返します。

この回答 on stackoverflow は、可能な解決策を示し、値が -1 になる理由を説明しています:なぜ [response expectedContentLength] return -1 .

私は試した:

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:_downloadURL] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:60];
[request setValue:@"" forHTTPHeaderField:@"Accept-Encoding"]; //Don't use Gzip to see total size of download

しかし、それは何も変わりません。expectedContentLength が値 -1 を与える理由を知っている人はいますか? 繰り返しますが、私が試した時間の半分しか与えられません。たまにはオケです。

EDIT コードを使用すると、DidReceiveResponseに[response AllHeaderFiles]を記録しました

[request setValue:@"" forHTTPHeaderField:@"Accept-Encoding"];

"Content-Type" = "text/html";
Date = "Mon, 16 Sep 2013 12:43:54 GMT";
Server = "Apache/2";
"Transfer-Encoding" = Identity;
Vary = "Accept-Encoding,User-Agent";
"X-Powered-By" = "PHP/5.2.17";

これは、そのコードなしの出力です。

Connection = "keep-alive";
"Content-Encoding" = gzip;
"Content-Length" = 1500;
"Content-Type" = "text/html";
Date = "Mon, 16 Sep 2013 12:38:56 GMT";
Server = "Apache/2";
Vary = "Accept-Encoding,User-Agent";
"X-Powered-By" = "PHP/5.2.17";

ただし、-1 を取得するかダウンロード サイズを取得するかに関係なく、両方の出力は同じままです。

4

0 に答える 0