ASIHTTPRequest详解 [经典3]
2015-09-23 09:11
836 查看
大文件断点续传
0.94 以后支持大文件的断点下载,只需要设置: [ request setAllowResumeForFileDownloads:YES ]; [ request setDownloadDestinationPath:downloadPath ]。 就可以了。ASIHTTPRequest会自动保存访问过的URL信息,并备之后用。在以下几个场景非常有用: 1,当没有网络连接的时候。 2,已下载的数据再次请求时,仅当它与本地版本不样时才进行下载。
ASIDownloadCache 设置下载缓存
它对Get请求的响应数据进行缓存(被缓存的数据必需是成功的200请求):
[ASIHTTPRequest setDefaultCache:[ASIDownloadCache sharedCache]]; |
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; [request setDownloadCache:[ASIDownloadCache sharedCache]]; |
仅仅需要创建不同的ASIDownloadCache,并设置缓存所使用的路径,并设置到需要使用的request实例中:
ASIDownloadCache *cache = [[[ASIDownloadCache alloc] init] autorelease]; [cache setStoragePath:@ "/Users/ben/Documents/Cached-Downloads" ]; [self setMyCache:cache]; ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; [request setDownloadCache:[self myCache]]; |
缓存策略是我们控制缓存行为的主要方式,如:什么时候进行缓存,缓存数据的利用方式。 以下是策略可选列表(可组合使用):
ASIUseDefaultCachePolicy | 这是一个默认的缓存策略“ASIAskServerIfModifiedWhenStaleCachePolicy”,这个很明白,见名知意(它不能与其它策略组合使用) |
---|---|
ASIDoNotReadFromCacheCachePolicy | 所读数据不使用缓存 |
ASIDoNotWriteToCacheCachePolicy | 不对缓存数据进行写操作 |
ASIAskServerIfModifiedWhenStaleCachePolicy | 默 认缓存行为,request会先判断是否存在缓存数据。a, 如果没有再进行网络请求。 b,如果存在缓存数据,并且数据没有过期,则使用缓存。c,如果存在缓存数据,但已经过期,request会先进行网络请求,判断服务器版本与本地版本是 否一样,如果一样,则使用缓存。如果服务器有新版本,会进行网络请求,并更新本地缓存 |
ASIAskServerIfModifiedCachePolicy | 与默认缓存大致一样,区别仅是每次请求都会 去服务器判断是否有更新 |
ASIOnlyLoadIfNotCachedCachePolicy | 如果有缓存在本地,不管其过期与否,总会拿来使用 |
ASIDontLoadCachePolicy | 仅当有缓存的时候才会被正确执行,如果没有缓存,request将被取消(没有错误信息) |
ASIFallbackToCacheIfLoadFailsCachePolicy | 这个选项经常被用来与其它选项组合使用。请求失败时,如果有缓存当网络则返回本地缓存信息(这个在处理异常时非常有用) |
如果设置了“defaultCachePolicy”则所有的请求都会使用此缓存。 |
你 可以设置缓存的数据需要保存多长时间,ASIHTTPRequest提供了两种策略: a,ASICacheForSessionDurationCacheStoragePolicy,默认策略,基于session的缓存数据存储。当下次 运行或[ASIHTTPRequest clearSession]时,缓存将失效。 b,ASICachePermanentlyCacheStoragePolicy,把缓存数据永久保存在本地, 如:
ASIHTTPRequest *request = [ ASIHTTPRequest requestWithURL:url ]; [ request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy ]; |
缓存其它特性
设置是否按服务器在Header里指定的是否可被缓存或过期策略进行缓存:
[[ ASIDownloadCache sharedCache ] setShouldRespectCacheControlHeaders:NO ]; |
[ request setSecondsToCache: 60 * 60 * 24 * 30 ]; // 缓存30天 |
[ request didUseCachedResponse ]; |
[ request setDownloadDestinationPath:[[ ASIDownloadCache sharedCache ] pathToStoreCachedResponseDataForRequest:request ]]; |
只要简单的实现ASICacheDelegate接口就可以被用来使用。
使用代理请求
默认的情况下,ASIHTTPRequest会使用被设置的默认代理。但你也可以手动修改http代理:
// Configure a proxy server manually NSURL *url = [ NSURL URLWithString:@ "http://allseeing-i.com/ignore" ]; ASIHTTPRequest *request = [ ASIHTTPRequest requestWithURL:url ]; [ request setProxyHost:@ "192.168.0.1" ]; [ request setProxyPort: 3128 ]; // Alternatively, you can use a manually-specified Proxy Auto Config file (PAC) // (It's probably best if you use a local file) [request setPACurl:[NSURL URLWithString:@ "file:///Users/ben/Desktop/test.pac" ]]; |
iOS4中,当应用后台运行时仍然请求数据:
[ request setShouldContinueWhenAppEntersBackground:YES ]; |
[ ASIHTTPRequest isNetworkInUse ] |
[ ASIHTTPRequest setShouldUpdateNetworkActivityIndicator:NO ]; |
[ request setNumberOfTimesToRetryOnTimeout: 2 ]; |
[ request setPersistentConnectionTimeoutSeconds:
120
];
// Disable persistent connections entirely
[ request setShouldAttemptPersistentConnection:NO ];
http://blog.sina.com.cn/s/blog_801997310101bi9s.html
相关文章推荐
- 浅谈HTTP中Get与Post的区别
- AFNetworking判断当前手机的网络状态
- ASIHTTPRequest详解 [经典]
- ASIHTTPRequest-插件的使用
- C# 程序员最常犯的 10 个错误http://www.oschina.net/translate/top-10-mistakes-that-c-sharp-programmers-make
- Linux管道编程实例 http://blog.csdn.net/pcliuguangtao/article/details/6453743
- 通过QQ查看对方的ip地址
- RHCSA 系列(八): 加固 SSH,设定主机名及启用网络服务
- 关于HTTPS://不能访问的问题
- TCP/IP协议详解
- TCP/IP 寻址(二)
- hiho 1228 : Mission Impossible 6,北京网络赛B题
- 【计算机网络】:经典计算题整理
- 2015北京网络赛 Couple Trees 倍增算法
- Android 网络编程
- corosync和pacemaker实现httpd和mysql双集群
- TCP/IP 简介(一)
- HDU 4002 Find the maximum (欧拉函数-积性函数的性质(2011年大连赛区网络赛第二题)
- 网络通信协议的一点理解
- RST复位报文段