您的位置:首页 > 理论基础 > 计算机网络

ASIHttpRequest 摘要

2015-07-08 20:05 615 查看
向服务器端上传数据

ASIFormDataRequest ,模拟 Form表单提交,其提交格式与 Header会自动识别。

没有文件:application/x-www-form-urlencoded

有文件:multipart/form-data
[align=left]ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];[/align]
[align=left][request setPostValue:@"Ben" forKey:@"first_name"];[/align]
[align=left][request setPostValue:@"Copsey" forKey:@"last_name"];[/align]
[align=left][request setFile:@"/Users/ben/Desktop/ben.jpg" forKey:@"photo"];[/align]
[align=left][request addData:imageData withFileName:@"george.jpg" andContentType:@"image/jpeg" forKey:@"photos"];[/align]
如果要发送自定义数据:
[align=left]ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];[/align]
[align=left][request appendPostData:[@"This is my data" dataUsingEncoding:NSUTF8StringEncoding]];[/align]
[align=left]// Default becomes POST when you use appendPostData: / appendPostDataFromFile: / setPostBody:[/align]
[align=left][request setRequestMethod:@"PUT"];[/align]
下载文件

通过设置request的setDownloadDestinationPath,可以设置下载文件用的下载目标目录。

首先,下载过程文件会保存在temporaryFileDownloadPath目录下。如果下载完成会做以下事情:

1,如果数据是压缩的,进行解压,并把文件放在downloadDestinationPath目录中,临时文件被删除

2,如果下载失败,临时文件被直接移到downloadDestinationPath目录,并替换同名文件。

如果你想获取下载中的所有数据,可以实现delegate中的request:didReceiveData:方法。但如果你实现了这个方法,request在下载完后,request并不把文件放在downloadDestinationPath中,需要手工处理。

获取响应信息

信息:status , header, responseEncoding
[align=left][request responseStatusCode];[/align]
[align=left][[request responseHeaders] objectForKey:@"X-Powered-By"];[/align]
[align=left][request responseEncoding];[/align]
获取请求进度

有两个回调方法可以获取请求进度,

1,downloadProgressDelegate,可以获取下载进度

2,uploadProgressDelegate,可以获取上传进度

cookie的支持

如果Cookie存在的话,会把这些信息放在NSHTTPCookieStorage容器中共享,并供下次使用。

你可以用[ ASIHTTPRequest setSessionCookies:nil ] ; 清空所有Cookies。

当然,你也可以取消默认的Cookie策略,而使自定义的Cookie:
[align=left]//Create a cookie[/align]
[align=left]NSDictionary *properties = [[[NSMutableDictionary alloc] init] autorelease];[/align]
[align=left][properties setValue:[@"Test Value" encodedCookieValue] forKey:NSHTTPCookieValue];[/align]
[align=left][properties setValue:@"ASIHTTPRequestTestCookie" forKey:NSHTTPCookieName];[/align]
[align=left][properties setValue:@".allseeing-i.com" forKey:NSHTTPCookieDomain];[/align]
[align=left][properties setValue:[NSDate dateWithTimeIntervalSinceNow:60*60] forKey:NSHTTPCookieExpires];[/align]
[align=left][properties setValue:@"/asi-http-request/tests" forKey:NSHTTPCookiePath];[/align]
[align=left]NSHTTPCookie *cookie = [[[NSHTTPCookie alloc] initWithProperties:properties] autorelease];[/align]

[align=left]//This url will return the value of the 'ASIHTTPRequestTestCookie' cookie[/align]
[align=left]url = [NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/read_cookie"];[/align]
[align=left]request = [ASIHTTPRequest requestWithURL:url];[/align]
[align=left][request setUseCookiePersistence:NO];[/align]
[align=left][request setRequestCookies:[NSMutableArray arrayWithObject:cookie]];[/align]
[align=left][request startSynchronous];[/align]

[align=left]//Should be: I have 'Test Value' as the value of 'ASIHTTPRequestTestCookie'[/align]
[align=left]NSLog(@"%@",[request responseString]);[/align]
大文件断点续传

0.94以后支持大文件的断点下载,只需要设置:

[ request setAllowResumeForFileDownloads:YES ];

[ request setDownloadDestinationPath:downloadPath ];

就可以了。

ASIHTTPRequest会自动保存访问过的URL信息,并备之后用。在以下几个场景非常有用:

1,当没有网络连接的时候。

2,已下载的数据再次请求时,仅当它与本地版本不样时才进行下载。

ASIDownloadCache 设置下载缓存

它对Get请求的响应数据进行缓存(被缓存的数据必需是成功的200请求):
[align=left][ASIHTTPRequest setDefaultCache:[ASIDownloadCache sharedCache]];[/align]
当设置缓存策略后,所有的请求都被自动的缓存起来。

另外,如果仅仅希望某次请求使用缓存操作,也可以这样使用:
[align=left]ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];[/align]
[align=left][request setDownloadCache:[ASIDownloadCache sharedCache]];[/align]
多种的缓存并存

仅仅需要创建不同的ASIDownloadCache,并设置缓存所使用的路径,并设置到需要使用的request实例中:
[align=left]ASIDownloadCache *cache = [[[ASIDownloadCache alloc] init] autorelease];[/align]
[align=left][cache setStoragePath:@"/Users/ben/Documents/Cached-Downloads"];[/align]
[align=left][self setMyCache:cache];[/align]
[align=left]ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];[/align]
[align=left][request setDownloadCache:[self myCache]];[/align]
缓存策略

缓存策略是我们控制缓存行为的主要方式,如:什么时候进行缓存,缓存数据的利用方式。

以下是策略可选列表(可组合使用):
[align=left]ASIUseDefaultCachePolicy[/align]
[align=left]这是一个默认的缓存策略“ASIAskServerIfModifiedWhenStaleCachePolicy”,这个很明白,见名知意(它不能与其它策略组合使用)[/align]
[align=left]ASIDoNotReadFromCacheCachePolicy[/align]
[align=left]所读数据不使用缓存[/align]
[align=left]ASIDoNotWriteToCacheCachePolicy[/align]
[align=left]不对缓存数据进行写操作[/align]
[align=left]ASIAskServerIfModifiedWhenStaleCachePolicy[/align]
[align=left]默认缓存行为,request会先判断是否存在缓存数据。a, 如果没有再进行网络请求。 b,如果存在缓存数据,并且数据没有过期,则使用缓存。c,如果存在缓存数据,但已经过期,request会先进行网络请求,判断服务器版本与本地版本是否一样,如果一样,则使用缓存。如果服务器有新版本,会进行网络请求,并更新本地缓存[/align]
[align=left]ASIAskServerIfModifiedCachePolicy[/align]
[align=left]与默认缓存大致一样,区别仅是每次请求都会 去服务器判断是否有更新[/align]
[align=left]ASIOnlyLoadIfNotCachedCachePolicy[/align]
[align=left]如果有缓存在本地,不管其过期与否,总会拿来使用[/align]
[align=left]ASIDontLoadCachePolicy[/align]
[align=left]仅当有缓存的时候才会被正确执行,如果没有缓存,request将被取消(没有错误信息)[/align]
[align=left]ASIFallbackToCacheIfLoadFailsCachePolicy[/align]
[align=left]这个选项经常被用来与其它选项组合使用。请求失败时,如果有缓存当网络则返回本地缓存信息(这个在处理异常时非常有用)[/align]
[align=left][/align]
[align=left]如果设置了“defaultCachePolicy”则所有的请求都会使用此缓存。[/align]
缓存存储方式

你可以设置缓存的数据需要保存多长时间,ASIHTTPRequest提供了两种策略:

a,ASICacheForSessionDurationCacheStoragePolicy,默认策略,基于session的缓存数据存储。当下次运行或[ASIHTTPRequest clearSession]时,缓存将失效。

b,ASICachePermanentlyCacheStoragePolicy,把缓存数据永久保存在本地,

如:
[align=left]ASIHTTPRequest *request = [ ASIHTTPRequest requestWithURL:url ];[/align]
[align=left][ request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy ];[/align]
另外,也可以使用clearCachedResponsesForStoragePolicy来清空指定策略下的缓存数据。

缓存其它特性

设置是否按服务器在Header里指定的是否可被缓存或过期策略进行缓存:
[align=left][[ ASIDownloadCache sharedCache ] setShouldRespectCacheControlHeaders:NO ];[/align]
设置request缓存的有效时间:
[align=left][ request setSecondsToCache:60*60*24*30 ]; // 缓存30天[/align]
可以判断数据是否从缓存读取:
[align=left][ request didUseCachedResponse ];[/align]
设置缓存所使用的路径:
[align=left][ request setDownloadDestinationPath:[[ ASIDownloadCache sharedCache ] pathToStoreCachedResponseDataForRequest:request ]];[/align]
实现自定义的缓存

只要简单的实现ASICacheDelegate接口就可以被用来使用。

使用代理请求

默认的情况下,ASIHTTPRequest会使用被设置的默认代理。但你也可以手动修改http代理:
[align=left]// Configure a proxy server manually[/align]
[align=left]NSURL *url = [ NSURL URLWithString:@"http://allseeing-i.com/ignore" ];[/align]
[align=left]ASIHTTPRequest *request = [ ASIHTTPRequest requestWithURL:url ];[/align]
[align=left][ request setProxyHost:@"192.168.0.1" ];[/align]
[align=left][ request setProxyPort:3128 ];[/align]

[align=left]// Alternatively, you can use a manually-specified Proxy Auto Config file (PAC)[/align]
[align=left]// (It's probably best if you use a local file)[/align]
[align=left][request setPACurl:[NSURL URLWithString:@"file:///Users/ben/Desktop/test.pac"]];[/align]
ASIHTTPRequest, 请求的其它特性

iOS4中,当应用后台运行时仍然请求数据:
[align=left][ request setShouldContinueWhenAppEntersBackground:YES ];[/align]
是否有网络请求:
[align=left][ ASIHTTPRequest isNetworkInUse ][/align]
是否显示网络请求信息在status bar上:
[align=left][ ASIHTTPRequest setShouldUpdateNetworkActivityIndicator:NO ];[/align]
设置请求超时时,设置重试的次数:
[align=left][ request setNumberOfTimesToRetryOnTimeout:2 ];[/align]
KeepAlive的支持:

// Set the amount of time to hang on to a persistent connection before it should expire to 2 minutes

[ request setPersistentConnectionTimeoutSeconds:120 ];

// Disable persistent connections entirely

[ request setShouldAttemptPersistentConnection:NO ];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: