ASIHTTPRequest 应用小结
来源:互联网 发布:量化宽松知乎 编辑:程序博客网 时间:2024/06/05 18:28
ASIHTTPRequest
http://allseeing-i.com/ASIHTTPRequest/How-to-use
1. 同步请求
- (IBAction)grabURL:(id)sender
{
NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request startSynchronous];
NSError *error = [request error];
if (!error) {
NSString *response = [request responseString];
}
}
忙等 直到获取返回 string
2. 异步请求
- (IBAction)grabURLInBackground:(id)sender
{
NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request startAsynchronous];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
// Use when fetching text data
NSString *responseString = [request responseString];
// Use when fetching binary data
NSData *responseData = [request responseData];
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
}
(1) 设置好 回到的 代理 [request setDelegate:self];
(2)实现回调处理
3. 使用 blocks
__block ASIHTTPRequest *request 声明的时候增加 __block 保证block不持有该请求
setCompletionBlock setFailedBlock
4. 使用 NSOperationQueue
声明一个queue
if (![self queue]) {
[self setQueue:[[[NSOperationQueue alloc] init] autorelease]];
}
请求添加到queue
设置好回调requestDone requestWentWrong
[[self queue] addOperation:request];
5. 回调处理
给request添加标识
userInfo
tag
尽量避免使用 URL作为标示,因为他可能随着重定向变化
ASINetworkQueues
对NSOperationQueue 进行了扩展
1. 跟踪上传下载过程 精确到byte
2. 增加更多回调
注意事项:
1. 当有任务失败,默认会停止queue中所有的请求,可以关闭这个设置
[queue setShouldCancelAllRequestsOnFailure:NO] -> shouldCancelAllRequestsOnFailure
取消请求[request cancel],同步请求无法关闭,取消请求会被当作一次错误来处理 failure delegate method ,clearDelegatesAndCancel 可以取消这个设置
2. request 不会持有他的代理,所以当控制器被释放掉[request clearDelegatesAndCancel];
6. 发送头部信息
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request addRequestHeader:@"Referer" value:@"http://allseeing-i.com/"];
ASIFormDataRequest 扩展了的 ASIHTTPRequest
7. 上传 binary data or files
- Form content types
- application/x-www-form-urlencoded
- multipart/form-data
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:@"Ben" forKey:@"first_name"];
[request setPostValue:@"Copsey" forKey:@"last_name"];
[request setFile:@"/Users/ben/Desktop/ben.jpg" forKey:@"photo"];
文件格式会根据给的url自动探测
8. 下载文件
可以直接下载文件到 file,不需占用内存
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDownloadDestinationPath:@"/Users/ben/Desktop/my_file.txt"];
下载过程 文件存放在 temporaryFileDownloadPath
下载完成后:
1. 如果是gzip ,解压downloadDestinationPath
2. 如果是普通文件,直接覆盖到downloadDestinationPath
最后删除temporaryFileDownloadPath
9. 处理跳转
遇到如下status codes 直接跳转
- 301 Moved Permanently
- 302 Found
- 303 See Other
responseHeaders / responseCookies / responseData / responseString 也是跳转后的页面
关闭跳转 shouldRedirect = NO
downloadProgressDelegate
request:didReceiveBytes
request:incrementDownloadSizeBy 自定义下载反馈尺度
跟中进度
对于需要验证的web service 推荐使用 useSessionPersistence
对于小于128KB的data是不可用的
回调 queueComplete
uploadProgressDelegates
request:didSendBytes
request:incrementUploadSizeBy
10. 处理 HTTP authentication
(1)
NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com/top_secret/"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setUsername:@"username"];
[request setPassword:@"password"];
(2)
使用keychain
iOS的keychain服务提供了一种安全的保存私密信息
[request setUseKeychainPersistence:YES];
(3)
使用session
[request setUseSessionPersistence:YES];
11. 断点续传
- (IBAction)resumeInterruptedDownload:(id)sender
{
NSURL *url = [NSURL URLWithString:
@"http://allseeing-i.com/ASIHTTPRequest/Tests/the_great_american_novel.txt"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
NSString *downloadPath = @"/Users/ben/Desktop/my_work_in_progress.txt";
// The full file will be moved here if and when the request completes successfully
[request setDownloadDestinationPath:downloadPath];
// This file has part of the download in it already
[request setTemporaryFileDownloadPath:@"/Users/ben/Desktop/my_work_in_progress.txt.download"];
[request setAllowResumeForFileDownloads:YES];
[request startSynchronous];
//The whole file should be here now.
NSString *theContent = [NSString stringWithContentsOfFile:downloadPath];
}
(1). 临时文件会在realloced can cel 的时候自动删除
(2). 续传是通过添加 http header Range: bytes=x 所以要确保服务器支持该功能
12. 上传大文件 不经过内存
(1) 使用ASIFormDataRequests
NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com/ignore"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:@"foo" forKey:@"post_var"];
[request setFile:@"/Users/ben/Desktop/bigfile.txt" forKey:@"file"];
[request startSynchronous];
(2) 普通请求的话设置:
[request setShouldStreamPostDataFromDisk:YES];
NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com/ignore"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setShouldStreamPostDataFromDisk:YES];
[request appendPostData:myBigNSData];
[request appendPostDataFromFile:@"/Users/ben/Desktop/bigfile.txt"];
[request startSynchronous];
(3) 使用PUT 方法
NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com/ignore"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setRequestMethod:@"PUT"];
[request setPostBodyFilePath:@"/Users/ben/Desktop/another-big-one.txt"];
[request setShouldStreamPostDataFromDisk:YES];
[request startSynchronous];
13 使用cache
打开默认使用cache
[ASIHTTPRequest setDefaultCache:[ASIDownloadCache sharedCache]];
手动使用cache
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDownloadCache:[ASIDownloadCache sharedCache]];
设置cache路径
ASIDownloadCache *cache = [[[ASIDownloadCache alloc] init] autorelease];
[cache setStoragePath:@"/Users/ben/Documents/Cached-Downloads"];
持有cache:
[self setMyCache:cache];
设置缓存策略
[request setCachePolicy:ASIAskServerIfModifiedCachePolicy|ASIFallbackToCacheIfLoadFailsCachePolicy];
继承ASICacheDelegate
自己实现cache
还有很多杂七杂八的功能
如Throttling bandwidth 限速功能
- ASIHTTPRequest 应用小结
- ASIHTTPRequest
- ASIHTTPRequest
- ASIHTTPRequest
- ASIHTTPRequest
- ASIHTTPRequest
- ASIHTTPRequest
- ASIHTTPRequest
- asihttprequest
- ASIHTTPRequest
- ASIHTTPRequest
- ASIHTTPRequest
- ASIHttpRequest
- ASIHTTPRequest
- ASIHttpRequest
- ASIHTTPRequest
- ASIHTTPRequest
- ASIHTTPRequest
- VC笔记 unicode、ascii、16进制bytedata 互转
- 堆和栈的区别(3)
- GCC 上的预定义宏
- 飞信-文档-开发者-API文档
- POJ 2142 The Balance
- ASIHTTPRequest 应用小结
- Nexus入门
- FFMpeg的解码流程
- Android 笔记
- ORACLE 多表关联 UPDATE 语句
- 如何计算系统容量
- eclipse中查看某个类是否被引用过的方法
- oracle 10g数据迁移到11g
- android JSON解析