使用ASIHTTPRequest 网络连接
2013-11-28 17:36
351 查看
#import <UIKit/UIKit.h> #import "ASIHTTPRequest.h" @interface WebImageView : UIImageView <ASIHTTPRequestDelegate> - (void)setImageURL:(NSURL *)url; @end #import "WebImageView.h" #import "ASIHTTPRequest.h" #import "ASIFormDataRequest.h" @implementation WebImageView - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code } return self; } - (void)setImageURL:(NSURL *)url { //同步请求 //[self synchronous:url]; //异步 // [self aSynchronous:url]; [self aSynchronousBlock:url]; } //同步请求 - (void)synchronous:(NSURL *)url { ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; [request setRequestMethod:@"GET"]; [request setTimeOutSeconds:60]; //设置请求头 //request setRequestHeaders:(NSMutableDictionary *) //发送同步请求 [request startSynchronous]; NSError *error = request.error; if (error != nil){ NSLog(@"error :%@", error); }else{ NSData *data = [request responseData]; self.image = [UIImage imageWithData:data]; } } //异步 - (void)aSynchronous:(NSURL *)url { ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; [request setRequestMethod:@"GET"]; [request setTimeOutSeconds:60]; //发送同步请求 request.delegate = self; [request startAsynchronous]; } //使用block, 不用设置代理 - (void)aSynchronousBlock:(NSURL *)url { ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; [request setRequestMethod:@"GET"]; [request setTimeOutSeconds:60]; //网络请求结束调用 [request setCompletionBlock:^{ NSData *data = [request responseData]; self.image = [UIImage imageWithData:data]; }]; [request setFailedBlock:^{ NSLog(@"error: %@", request.error); }]; //发送异步请求 [request startAsynchronous]; } //post 请求参数 网络请求 ASIFormDataRequest - (void)aSynchronousPost:(NSURL *)url { ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; [request setRequestMethod:@"POST"]; [request setTimeOutSeconds:60]; //设置请求参数 //username=gkgk&pwd=3333 //[request addPostValue:@"" forKey:@""]//比如用户名和密码等。 //网络请求结束调用 [request setCompletionBlock:^{ NSData *data = [request responseData]; self.image = [UIImage imageWithData:data]; }]; [request setFailedBlock:^{ NSLog(@"error: %@", request.error); }]; //发送异步请求 [request startAsynchronous]; } #pragma mark ASIHttpRequest delegate - (void)requestFinished:(ASIHTTPRequest *)request { NSData *data = [request responseData]; self.image = [UIImage imageWithData:data]; } - (void)requestFailed:(ASIHTTPRequest *)request { NSLog(@"error :%@", request.error); } @end
缓存
//使用block, 不用设置代理
- (void)aSynchronousBlock:(NSURL *)url
{
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setRequestMethod:@"GET"];
[request setTimeOutSeconds:60];
//网络请求结束调用
[request setCompletionBlock:^{
NSData *data = [request responseData];
self.image = [UIImage imageWithData:data];
}];
// 使用缓存策略
//缓存位置
NSString *cachePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
ASIDownloadCache *cache = [[ASIDownloadCache alloc]init];
[cache setStoragePath:cachePath];
//缓存策略
cache.defaultCachePolicy = ASIOnlyLoadIfNotCachedCachePolicy;
//每次启动,清除上次缓存文件
request.cacheStoragePolicy = ASICacheForSessionDurationCacheStoragePolicy;
//缓存对象
request.downloadCache = cache;
[request setFailedBlock:^{
NSLog(@"error: %@", request.error);
}];
//发送异步请求
[request startAsynchronous];
}
判断数据来自缓存,还是网络
//使用block, 不用设置代理
- (void)aSynchronousBlock:(NSURL *)url
{
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setRequestMethod:@"GET"];
[request setTimeOutSeconds:60];
//网络请求结束调用
[request setCompletionBlock:^{
NSData *data = [request responseData];
self.image = [UIImage imageWithData:data];
//判断数据来自网路,还是缓存
if (request.didUseCachedResponse){
NSLog(@"数据来自缓存");
}else{
NSLog(@"数据来自网络");
}
}];
// 使用缓存策略
//缓存位置
NSString *cachePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
ASIDownloadCache *cache = [[ASIDownloadCache alloc]init];
[cache setStoragePath:cachePath];
//缓存策略
cache.defaultCachePolicy = ASIOnlyLoadIfNotCachedCachePolicy;
//每次启动,清除上次缓存文件
request.cacheStoragePolicy = ASICacheForSessionDurationCacheStoragePolicy;
//缓存对象
request.downloadCache = cache;
[request setFailedBlock:^{
NSLog(@"error: %@", request.error);
}];
//发送异步请求
[request startAsynchronous];
}
需要添加ASIHTtpRequestSample开源文件以及
libz.dylib
libxml2.dylib
moblieCoreServices.framework
systemConfigration.framework
CFNetwork.framework
点击打开链接
相关文章推荐
- IOCP模型与网络编程
- IOCP工作原理
- 基于多线程的大容量MMORPG主逻辑服务器实现策略
- 通用高性能 Windows Socket 组件 HP-Socket
- 网络编程基础
- 服务器网络性能调优的尝试,以失败告终
- 使用nginx搭建https服务器
- 使用nginx搭建https服务器
- android之通过URL来获取网络资源并下载资源简单实例
- 简单负载均衡 和 IBM HTTP Server配置
- iOS开发ASIHttpRequest发送数据
- 配置扩展的访问控制列表------允许tcp协议通过出站端口访问服务器和禁止icmp协议通过出站端口访问服务器
- InitializeCriticalSectionAndSpinCount
- 游戏服务器结构探讨
- F5加入OpenStack社区 推动4-7层网络服务演进
- C语言 字符串处理函数 转自 http://blog.chinaunix.net/uid-25885064-id-3175049.html
- TCP传输连接有限状态机转换机制
- 关于主机多IP和服务端口绑定IP的研究
- http://codeforces.com/contest/368/problem/D
- Linux与网络通信:基于TCP的网络聊天室