大文件斷點續傳
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 ]; |
另外,也可以使用clearCachedResponsesForStoragePolicy來清空指定策略下的緩存數據。
緩存其它特性
設置是否按服務器在Header里指定的是否可被緩存或過期策略進行緩存:
[[ ASIDownloadCache sharedCache ] setShouldRespectCacheControlHeaders:NO ]; |
設置request緩存的有效時間:
[ 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" ]]; |
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 ];