ASIHttpRequest的介绍及使用
来源:互联网 发布:好玩的手机rpg 知乎 编辑:程序博客网 时间:2024/06/05 04:07
ASI类库简介和使用说明
ASIHttpRequest 是一款Http访问开源项目,让简单的API实现复杂的功能.
主要功能:
1. 向服务器端提交数据,获取数据.
2. 获取上传和下载的进度信息
3. 当应用(ios+)在后台运行时,请求可以继续进行
4. 支持GZIP压缩数据
5. 可以缓存请求返回的数据
6. 可以下载完整的网页,任意大小的页面都可以无限期缓存.
7. 支持客户端证书
8. 支持通过代理发起Http请求
9. 支持带宽限制
10.支持断点续传
11.支持同步和异步请求
一.首先需要在Xcode项目中添加第三方类库文件
(1)非ARC
添加以下框架
commond+B如果编译没有错误就可以写代码了
(2)arc
因为ASIHttpRequest文件中的一些文件不支持ARC,可以在Project Setting ->Build Phases中,对这些文件选中-fno-objc-arc标志.这样Xcode编译项目时,这些文件就不会使用ARC
二.代码
同步代码://因为我们只是要获得一张图片,不需要此时只能使用GET请求向服务器提供数据,
/*ASIFormDataRequest与ASIHttpRequest
ASIFormDataRequest这个类是继承自ASIHttpRequest的, 只不过你用ASiFormDataRequest默认的method是post你需要设置一下,而ASIHttpRequest默认就是get. 如果是 Get ,或者参数可以简单的组成 Get 方式提交的时候就用 ASIHTTPRequest 就可以了
原因是简单,如果必须是 POST ,或者参数很多,你要构造一个带参数的 URL 很麻烦的时候,就用 ASIHttpFormDataRequest
*/
ASIHTTPRequest *request = [ASIHTTPRequestrequestWithURL:url];
[request setRequestMethod:@"GET"];//设置请求方式
[request setTimeOutSeconds:60];//设置超时时间
// //发起同步请求
[request startSynchronous];
NSError *error = request.error;
if (error == nil) {
NSData data = request.responseData;/*此处要获取返回的文本信息,调用responseString方法,除此之外都要调用responseData
NSString *responseString = [requestresponseString];
NSData *responseData = [requestresponseData];
*/
self.image = [UIImageimageWithData:data];
}else
{
NSLog(@"请求数据失败");
}
异步请求:
第一种方法
ASIHTTPRequest *request= [ASIHTTPRequestrequestWithURL:url];
[request setRequestMethod:@"GET"];
[request setTimeOutSeconds:60];
//设置代理
//设置网络请求完成后调用的block
[request setCompletionBlock:^{
self.image = [UIImageimageWithData:request.responseData];
}];
//设置网络请求失败调用的block
[request setFailedBlock:^{
NSLog(@"请求网络数据出错%@",request.error);
}];
[request startAsynchronous];
第二种方法
ASIHTTPRequest *request= [ASIHTTPRequestrequestWithURL:url];
[request setRequestMethod:@"GET"];
[request setTimeOutSeconds:60];
//设置代理
request.delegate = self;
[requeststartAsynchronous];
要在.h文件中声明ASIHTTPRequestDelegate
代理方法:
//请求数据完成后调用
- (void)requestFinished:(ASIHTTPRequest *)request
{
self.image = [UIImageimageWithData:request.responseData];
}
//请求数据失败调用
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSLog(@"请求网络数据出错%@",request.error);
}
三.ASI缓存的使用
如果设置了缓存,当下次请求时,如果本地有缓存则使用本地的缓存
NSString *cachePath = [NSHomeDirectory()stringByAppendingPathComponent:@"Documents"];
NSLog(@"%@",cachePath);
//创建缓存对象
ASIDownloadCache *cache = [[ASIDownloadCachealloc] init];
//设置缓存路径
[cache setStoragePath:cachePath];
//设置缓存策略
[cache setDefaultCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy];
//设置request缓存周期
//ASICacheForSessionDurationCacheStoragePolicy:设置每次请求会话时将上次的缓存文件清除掉
//ASICachePermanentlyCacheStoragePolicy:是持久缓存,程序下次启动,缓存仍然还在
[request setCacheStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy];
//设置请求应用的缓存对象
request.downloadCache = cache;
四.post 上传图片 需要有自己的
UIImage * img=[UIImageimageNamed:@"a"];
NSData * data=UIImagePNGRepresentation(img);//获取图片数据
/*
ios中获取图片的方法有两种,一种是UIImageJPEGRepresentation,一种是UIImagePNGRepresentation
前者获取到图片的数据量要比后者的小很多。。
*/
NSMutableData *imageData = [NSMutableDatadataWithData:data];//ASIFormDataRequest的setPostBody方法需求的为NSMutableData类型
NSURL *url = [NSURLURLWithString:@"http://user.qzone.qq.com/862297751/4"];
ASIFormDataRequest *aRequest = [[ASIFormDataRequestalloc] initWithURL:url];
[aRequest setDelegate:self];//代理
[aRequest setRequestMethod:@"POST"];
[aRequest setPostValue:@"862297751"forKey:@"username"];
[aRequest addPostValue:@"201202150904"forKey:@"password"];
[aRequest setPostBody:imageData];
//[aRequestaddRequestHeader:@"Content-Type"value:@"binary/octet-stream"];//这里的value值需与服务器端一致
[aRequest startAsynchronous];//开始。异步
[aRequest setDidFinishSelector:@selector(headPortraitSuccess)];//当成功后会自动触发headPortraitSuccess方法
[aRequest setDidFailSelector:@selector(headPortraitFail)];//如果失败会自动触发headPortraitFail方法
//[aRequest release];
- ASIHttpRequest的介绍及使用
- ASIHTTPRequest的使用——介绍说明
- 转载]ASIHTTPRequest使用介绍
- ASIHTTPRequest使用介绍
- ASIHTTPRequest使用介绍
- 开源库ASIHTTPRequest使用介绍
- ASIHTTPRequest使用介绍
- 开源库ASIHTTPRequest使用介绍
- ASIHTTPRequest 的介绍
- ASIHTTPRequest使用介绍 --进度显示
- iOS开发ASIHttpRequest介绍(五)--ASIHTTPRequest中Cookie的使用
- iOS开源库的学习-ASIHTTPRequest使用介绍
- ASIHTTPRequest使用过程中遇到的问题及解决办法
- ASIHTTPRequest使用过程中遇到的问题及解决办法
- ASIHTTPRequest使用过程中遇到的问题及解决办法
- ASIHTTPRequest使用过程中遇到的问题及解决办法
- ASIHTTPRequest的超详细介绍
- ASIHTTPRequest-Cookie的使用
- Linux虚拟机用ssh登录不了,解决方法。
- UNIX系统中目录权限和文件权限
- ASV格式文件
- DTree简单用法
- SQL Server 数据库没有有效所有者的三种解决办法
- ASIHttpRequest的介绍及使用
- CentOS_7.0 下安装Hive-0.13.1
- java I/O流常用代码
- huhamhire-hosts自动更新Google Hosts文件教程
- 从Oralce数据库(第三方)读出的Date数据,怎么转为为time_t在MCF控件CListCtrl控件中正确的显示时间
- 设计模式六大原则(3):依赖倒置原则
- js缓动滚向顶端和onmousemove鼠标跟随事件
- 对前沿的一些思考
- webapi 支持namespace