从0.94版本开始,ASIHTTPRequest可以恢复中断的下载
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | - (IBAction)resumeInterruptedDownload:(id)sender { NSURL *url = [NSURL URLWithString: ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; NSString *downloadPath = @"/Users/ben/Desktop/asi.png"; //当request完成时,整个文件会被移动到这里 [request setDownloadDestinationPath:downloadPath]; //这个文件已经被下载了一部分 [request setTemporaryFileDownloadPath:@"/Users/ben/Desktop/asi.png.download"]; [request setAllowResumeForFileDownloads:YES]; [request startSynchronous]; //整个文件将会在这里 NSString *theContent = [NSString stringWithContentsOfFile:downloadPath]; } |
这个特性只对下载数据到文件中有效,你必须为一下情况的request设置allowResumeForFileDownloads 为YES:
另外,你必须自己设置一个临时下载路径(setTemporaryFileDownloadPath),这个路径是未完成的数据的路径。新的数据将会被添加到这个文件,当下载完成时,这个文件将被移动到downloadDestinationPath 。
断点续传的工作原理是读取temporaryFileDownloadPath的文件的大小,并使用Range: bytes=x HTTP头来请求剩余的文件内容。
ASIHTTPRequest并不检测是否存在Accept-Ranges头(因为额外的HEAD头请求会消耗额外的资源),所以只有确定服务器支持断点续传下载时,再使用这个特性。
如果你请求的资源很大,你可以直接将数据下载到文件中来节省内存。此时,ASIHTTPRequest将不会一次将返回数据全部保持在内存中。
1 2 | ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; [request setDownloadDestinationPath:@"/Users/ben/Desktop/my_file.txt"]; |
当我们把数据下载到downloadDestinationPath时,数据将首先被存在临时文件中。此时文件的路径名存储在temporaryFileDownloadPath中。当request完成时,会发生下面两件事之一:
注意,如果服务器响应数据为空,那么文件是不会被创建的。如果你的返回数据可能为空,那么你应该先检查下载文件是否存在,再对文件进行操作。
如果你想处理服务器响应的数据(例如,你想使用流解析器对正在下载的数据流进行处理),你应该实现代理函数 request:didReceiveData:。注意如果你这么做了,ASIHTTPRequest将不会填充responseData到内存,也不会将数据写入文件(downloadDestinationPath )——你必须自己搞定这两件事(之一)。
ASIHTTPRequest并不对HTTP状态码做任何处理(除了重定向和授权状态码,下面会介绍到),所以你必须自己检查状态值并正确处理。
1 2 3 4 | ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; [request startSynchronous]; int statusCode = [request responseStatusCode]; NSString *statusMessage = [request responseStatusMessage]; |
1 2 3 4 | ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; [request startSynchronous]; NSString *poweredBy = [[request responseHeaders] objectForKey:@"X-Powered-By"]; NSString *contentType = [[request responseHeaders] objectForKey:@"Content-Type"]; |
ASIHTTPRequest会试图读取返回数据的编码信息(Content-Type头信息)。如果它发现了编码信息,它会将编码信息设定为合适 的 NSStringEncoding.如果它没有找到编码信息,它会将编码设定为默认编码(NSISOLatin1StringEncoding)。
当你调用[request responseString],ASIHTTPRequest会尝试以responseEncoding将返回的Data转换为NSString。
当遇到以下HTTP状态码之一时,ASIHTTPRequest会自动重定向到新的URL:
当发生重定向时,响应数据的值(responseHeaders,responseCookies,responseData,responseString等等)将会映射为最终地址的相应返回数据。
当URL发生循环重定向时,设置在这个URL上的cookie将被储存到全局域中,并在适当的时候随重定向的请求发送到服务器。
Cookies set on any of the urls encountered during a redirection cycle will be stored in the global cookie store, and will be represented to the server on the redirected request when appropriate.
你可以关闭自动重定向:将shouldRedirect设置为NO。
默认情况下,自动重定向会使用GET请求(请求体为空)。这种行为符合大多数浏览器的行为,但是HTTP spec规定301和302重定向必须使用原有方法。
要对301、302重定向使用原方法(包含请求体),在发起请求之前,设置shouldUseRFC2616RedirectBehaviour 为YES。
在ASIHTTPRequest中使用gzip
- (IBAction)grabURL:(id)sender
{
NSURL *url = [NSURL URLWithString:@"http://www.dreamingwish.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
// 默认为YES, 你可以设定它为NO来禁用gzip压缩
[request setAllowCompressedResponse:YES];
[request startSynchronous];
BOOL *dataWasCompressed = [request isResponseCompressed]; // 响应是否被gzip压缩过?
NSData *compressedResponse = [request rawResponseData]; // 压缩的数据
NSData *uncompressedData = [request responseData]; // 解压缩后的数据
NSString *response = [request responseString]; // 解压缩后的字符串
}
联系客服