ASIDownloadCache 设置下载缓存
它对Get请求的响应数据进行缓存(被缓存的数据必需是成功的200请求):
[ASIHTTPRequest setDefaultCache:[ASIDownloadCache sharedCache]];
当设置缓存策略后,所有的请求都被自动的缓存起来。
另外,如果仅仅希望某次请求使用缓存操作,也可以这样使用:
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDownloadCache:[ASIDownloadCache sharedCache]];
缓存存储方式
你可以设置缓存的数据需要保存多长时间,ASIHTTPRequest提供了两种策略:
a,ASICacheForSessionDurationCacheStoragePolicy,默认策略,基于session的缓存数据存储。当下次运行或[ASIHTTPRequest clearSession]时,缓存将失效。
b,ASICachePermanentlyCacheStoragePolicy,把缓存数据永久保存在本地,
如:
ASIHTTPRequest *request = [ ASIHTTPRequest requestWithURL:url ];
[ request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy ];
ASI 不支持arc 现在停止更新了 bug(请求超时后,如果没有实现timeout 这个函数,在他调用代理时 ASI没有进行判断,就会崩溃)
知识扩展:/*
网络传输 分为同步和异步
同步会卡当前线程,当前线程会停顿在同步,直到同步完成后,在继续执行
while (1) {
}
异步,请求会开启一条线程,在另外那条线程中进行操作数据,不会造成卡当前线程,当前线程流 程
网络请求 分为GET和POST
GET请求 http://www.baidu.com?categroy=1&name=zhangcheng;
参数直接追加在地址后面,在浏览器中,即可看到以上格式,称之为明文
优点:方便调试 在12306最开始阶段都是使用明文
缺点:不安全,所有信息,小白都能明白,传输最大字节数不超过128字节,超过的自动抛弃
POST请求http://www.baidu.com
参数使用body进行传输,用户看到不,浏览器也可不调试
优点:小白不会吐槽了,蛋疼的程序猿继续会吐槽
缺点:超麻烦
数据有时候需要进行加密
加密方式
MD5 一般默认在PC时代对用户登录密码进行加密
RC4
Sha-1
Sha-4
交通局采用RC4+Sha-1混合加密后在进行MD5加密MD5加密后,增加时间字符串,在此进行MD5加 密
*/
1.1 需加入4个库 SystemConfiguration.framework (如果没加会报错 Reachability文件错误) CFNetwork.framework MobileCoreServicrs.framework libz.dylib 1.2 //ASIHTTPRequest 不能进行POST请求,只能GET请求 1.2.1 添加头文件#import "ASIHTTPRequest.h" 定义全局指针 ASIHTTPRequest*request;
//初始化
request=[ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]];
//设置代理
request.delegate=self;
[request startAsynchronous];
1.2.2 代理的方法:
//请求失败
-(void)requestFailed:(ASIHTTPRequest *)request{
}
//请求成功
-(void)requestFinished:(ASIHTTPRequest *)request{
//请求完获得的结果可以是string也可以是data request.responseData; request.responseString; 1.如果请求的是图片 使用data 2.如果 请求的是数据 ,接口使用的是string 之后进行解析 3.如果请求网页直接加 载webView
UIWebView *webView=[[UIWebView alloc]initWithFrame:self.view.frame];
[webView loadHTMLString:request.responseString baseURL:[NSURL URLWithString:@"http://www.sina.com"]];
webView.tag=100;
[self.view addSubview:webView];
[webView release];
[self performSelector:@selector(showSina) withObject:nil afterDelay:10];
}
//此函数实现,ASI默认数据保存过程由程序猿自己进行,那么再请求成功后 request.responseString 或者data打印都会为nil 一般不要写这句话
-(void)request:(ASIHTTPRequest *)request didReceiveData:(NSData *)data{
}
1.3 ASIFormDataRequest GET和POST都可以进行请求继承于 ASIHTTPRequest 加入头文件:#import "ASIFormDataRequest.h" 代 理:ASIHTTPRequestDelegate
/*
POST请求:1.用于数据的保密
2.用于上传文件
2种方式上传文件:1.表单形式 多见于php的服务端
2.二进制形式 其他类型的服务端,PHP也可以用,但是服务端会得人不多
*/
1.3.1 ASIFormDataRequest *Request;
Request=[[ASIFormDataRequest alloc]initWithURL:[NSURL URLWithString:@""]];
Request.delegate=self;
//使用POST请求上传参数
[Request setPostValue:@"qianfeng567" forKey:@"uesrname"];
[Request setPostValue:@"123456" forKey:@"password"];
//如何POST上传文件 有2种格式一种是data 一种是路径
//1。使用data上传
UIImage *image=[UIImage imageNamed:@"1.png"];
NSData *data=UIImagePNGRepresentation(image);
//headImage是服务器端定义的key,具体要看服务端如何定义
[Request setData:data forKey:@"headImage"];
//2.使用路径
NSString *path=[[NSBundle mainBundle]pathForResource:@"1" ofType:@"png"];
[Request setFile:path forKey:@"hedaImage"];
//开始异步上传
[Request startAsynchronous];
1.3.2代理方法:
- (void)requestFinished:(ASIHTTPRequest *)request{
//是否上传成功
NSLog(@"。。。。。。。。%@",Request.responseString);
}
-(void)requestFailed:(ASIHTTPRequest *)request{
}
1.4 使用ASIFormDataRequest进行下载
加入头文件:#import "ASIFormDataRequest.h" 代理:ASIHTTPRequestDelegate
//下载文件有2种方式:
/*1.下载前就设定好保持目录,然后进行下载
2.下载后根据data数据写入指定目录
注:如果设定了保存目录,再请求成功的时候,string和data都为nil
最后一个ASI版本支持大文件下载,同时支持进度显示
*/
//http://192.168.109.101/share_1409/day22.zip
request=[[ASIFormDataRequest alloc]initWithURL:[NSURL URLWithString:@"http://192.168.109.101/share_1409/day22.zip"]];
request.delegate=self;
//显示进度
UIProgressView *progressView=[[UIProgressView alloc]initWithFrame:CGRectMake(20, 100, 300, 30)];
progressView.trackTintColor=[UIColor redColor];
progressView.progressTintColor=[UIColor greenColor];
[self.view addSubview:progressView];
[request setDownloadProgressDelegate:progressView];
//设置高精度下载
[request setShowAccurateProgress:YES];
//设置下载完成保存目录
NSString *path=[NSHomeDirectory() stringByAppendingString:@"/ Documents/11.zip"];
[request setDownloadDestinationPath:path];
//设置临时保存目录,下载完成后会自动删除
[request setTemporaryFileDownloadPath:[path stringByAppendingString:@".temp"]];
NSLog(@"~~~~~~~~%@",path);
//设置断点续传 如果一次没有下完 下次会从上次结束的位置直接开始
[request setAllowResumeForFileDownloads:YES];
//开始异步下载
[request startAsynchronous];
1.4.1 代理方法:-(void)requestFailed:(ASIHTTPRequest *)request{
}
-(void)requestFinished:(ASIHTTPRequest *)request{
NSLog(@"下载完成~~~~~%@",request.responseString);
}
1.5队列下载 加入头文件#import "ASIFormDataRequest.h"
#import "ASINetworkQueue.h" 代理:ASIHTTPRequestDelegate
// 创建队列
ASINetworkQueue *queue=[[ASINetworkQueue alloc]init];
//最大并行下载数
queue.maxConcurrentOperationCount=10;
//队列也需要设置下载精度 如果不设置,之后有0和1
[queue setShowAccurateProgress:YES];
//需要添加任务
for (int i=0; i<10; i++) {
//创建网络请求
ASIFormDataRequest *request=[[ASIFormDataRequest alloc]initWithURL:[NSURL URLWithString:@"http://192.168.109.101/share_1409/day22.zip"]];
request.delegate=self;
//断点续传 、进度下载、后天下载
//需要设置6个属性
//1.断点续传
[request setAllowResumeForFileDownloads:YES];
//保存目录
NSString *str=[NSHomeDirectory() stringByAppendingString:[NSString stringWithFormat:@"/Documents/download%d.zip",i]];
//2.保存下载完成后的目录
[request setDownloadDestinationPath:str];
//3.临时下载目录
[request setTemporaryFileDownloadPath:[str stringByAppendingString:@".temp"]];
//4.设置高精度下载
[request setShowAccurateProgress:YES];
//5。设置进度条的显示
UIProgressView *pro=[[UIProgressView alloc]initWithFrame:CGRectMake(10, 2 0+50*i, 300, 40)];
[request setDownloadProgressDelegate:pro];
[self.view addSubview:pro];
//6。设置后台下载
[request setShouldContinueWhenAppEntersBackground:YES];
[queue addOperation:request];
}
UIProgressView *queuePro=[[UIProgressView alloc]initWithFrame:CGRectMake(20, 500, 300, 30)];
[queue setDownloadProgressDelegate:queuePro];
[self.view addSubview:queuePro];
//队列开始运行
[queue go];