ASIHTTPRequest详解 【经典3】
来源:互联网 发布:powermill编程 编辑:程序博客网 时间:2024/05/30 04:24
ASIHTTPRequest详解 【经典3】
(2013-01-28 09:47:19)asihttprequest
ios
ios博客
ios基地
什么时候
分类: iOS大文件断点续传
0.94以后支持大文件的断点下载,只需要设置:
[ request setAllowResumeForFileDow
[ request setDownloadDestinationPa
就可以了。
ASIHTTPRequest会自动保存访问过的URL信息,并备之后用。在以下几个场景非常有用:
1,当没有网络连接的时候。
2,已下载的数据再次请求时,仅当它与本地版本不样时才进行下载。
ASIDownloadCache 设置下载缓存
它对Get请求的响应数据进行缓存(被缓存的数据必需是成功的200请求):
[ASIHTTPRequest setDefaultCache:[ASIDownloadCachesharedCache]];
当设置缓存策略后,所有的请求都被自动的缓存起来。
另外,如果仅仅希望某次请求使用缓存操作,也可以这样使用:
ASIHTTPRequest *request = [ASIHTTPRequestrequestWithURL:url];
[request setDownloadCache:[ASIDownloadCachesharedCache]];
多种的缓存并存
仅仅需要创建不同的ASIDownloadCache,并设置缓存所使用的路径,并设置到需要使用的request实例中:
ASIDownloadCache *cache = [[[ASIDownloadCache alloc] init]autorelease];
[cache setStoragePath:@
"/Users/ben/Documents/Cached-Downloads"
];
[self setMyCache:cache];
ASIHTTPRequest *request = [ASIHTTPRequestrequestWithURL:url];
[request setDownloadCache:[self myCache]];
缓存策略
缓存策略是我们控制缓存行为的主要方式,如:什么时候进行缓存,缓存数据的利用方式。
以下是策略可选列表(可组合使用):
缓存存储方式
你可以设置缓存的数据需要保存多长时间,ASIHTTPRequest提供了两种策略:
a,ASICacheForSessionDurati
b,ASICachePermanentlyCache
如:
ASIHTTPRequest *request = [ ASIHTTPRequest requestWithURL:url];
[ requestsetCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
另外,也可以使用clearCachedResponsesForS
缓存其它特性
设置是否按服务器在Header里指定的是否可被缓存或过期策略进行缓存:
[[ ASIDownloadCache sharedCache ]setShouldRespectCacheControlHeaders:NO ];
设置request缓存的有效时间:
[ request setSecondsToCache:
60
*
60
*
24
*
30
];
//缓存30天
可以判断数据是否从缓存读取:
[ request didUseCachedResponse ];
设置缓存所使用的路径:
[ request setDownloadDestinationPath:[[ ASIDownloadCachesharedCache ] 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 Configfile (PAC)
// (It's probably best if you use a local file)
[request setPACurl:[NSURL URLWithString:@
"file:///Users/ben/Desktop/test.pac"
]];
ASIHTTPRequest, 请求的其它特性
iOS4中,当应用后台运行时仍然请求数据:
[ request setShouldContinueWhenAppEntersBackground:YES];
是否有网络请求:
[ ASIHTTPRequest isNetworkInUse ]
是否显示网络请求信息在status ba
[ ASIHTTPRequest setShouldUpdateNetworkActivityIndicator:NO];
设置请求超时时,设置重试的次数:
[ request setNumberOfTimesToRetryOnTimeout:
2
];
KeepAlive的支持:
[ request setPersistentConnectionTimeoutSeconds:
120
];
// Disable persistent connections entirely
[ request setShouldAttemptPersistentConnection:NO ];
转自:zkdemon的专栏http://blog.sina.com.cn/s/blog_801997310101bi9s.html
- ASIHTTPRequest详解 【经典3】
- ASIHTTPRequest详解 【经典3】
- ASIHTTPRequest详解 【经典】
- ASIHTTPRequest详解 【经典】(一)
- ASIHTTPRequest详解 【经典】(二)
- ASIHTTPRequest详解 【经典】(三)
- ASIHTTPRequest详解 【经典2】
- ASIHttpRequest使用详解(3)
- ASIHTTPRequest详解
- ASIHTTPRequest 详解
- ASIHTTPRequest 详解
- ASIHTTPRequest 详解
- ASIHTTPRequest 详解
- ASIHTTPRequest 详解
- ASIHTTPRequest 详解
- ASIHTTPRequest 详解
- ASIHTTPRequest 详解
- ASIHTTPRequest 详解
- Android ListAdapter的高级功能
- python+flask实现简单博客
- 黑马程序员 java基础 函数 数组 查找与排序总结
- SQL查询~ 存在一个表而不在另一个表中的数据
- 函数指针
- ASIHTTPRequest详解 【经典3】
- WAS性能测试工具的使用
- ArcGISDynamicMapServiceLayer以ArcGISTiledMapServiceLaye方式使用
- 队列的简单实现
- ref和out
- Tomcat 系统架构与设计模式,第 1 部分: 工作原理(转载)
- 【转】对攻读计算机研究生的看法
- phoneGap+sencha touch开发工程
- 新的一年到来了,祝大家新年快了,蛇年吉祥。。。2013祝福你