ASIHTTPRequest
什么是ASI全称是ASIHTTPRequest,外号“HTTP终结者”,功能十分强大基于底层的CFNetwork框架,运行效率很高可惜作者早已停止更新,有一些潜在的BUG无人去解决很多公司的旧项目里面都残留着它的身影,以前的很多iOS项目都是ASI+SBJson会不会用ASI,可以算是检验是否为老牌iOS程序员的标准之一ASI的github地址https://g
// 1.创建请求
NSURL *url = [NSURL URLWithString:@"http://192.168.1.103:8080/MJServer/login?username=123&pwd=123"];
ASIHTTPRequest*request = [ASIHTTPRequest requestWithURL:url];
request.timeOutSeconds = 5; // 超时
// 2.发送同步请求
[request startSynchronous];
// 3.获得错误信息
NSError*error = [request error];
if(error) {
NSLog(@"出错了");
} else {
//获得服务器的响应
NSData*data = [request responseData];
} //[request responseData]
发送异步请求// 1.创建请求
NSURL *url = [NSURL URLWithString:@"http://192.168.1.103:8080/MJServer/login?username=123&pwd=123"];
ASIHTTPRequest*request = [ASIHTTPRequest requestWithURL:url];
request.timeOutSeconds = 5; // 超时
// 2.设置代理
request.delegate = self;
// 3.发送异步请求
[request startAsynchronous];
// ASI通过代理的方式处理异步请求,请求成功、失败都会通知代理
// 代理需要遵守ASIHTTPRequestDelegate协议
ASIHTTPRequestDelegate- (void)request:(ASIHTTPRequest*)request didReceiveData:(NSData*)data
- (void)requestFailed:(ASIHTTPRequest*)request
- (void)requestFinished:(ASIHTTPRequest*)request
[request clearDelegatesAndCancel];
ASI的SEL回调@property (atomic, assign) SEL didStartSelector;
@property (atomic, assign) SEL didReceiveResponseHeadersSelector;
@property (atomic, assign) SEL willRedirectSelector;
@property (atomic, assign) SEL didFinishSelector;
@property (atomic, assign) SEL didFailSelector;
@property (atomic, assign) SEL didReceiveDataSelector;
ASI的block回调- (void)setStartedBlock:(ASIBasicBlock)aStartedBlock;
- (void)setHeadersReceivedBlock:(ASIHeadersBlock)aReceivedBlock;
- (void)setCompletionBlock:(ASIBasicBlock)aCompletionBlock;
- (void)setFailedBlock:(ASIBasicBlock)aFailedBlock;
- (void)setBytesReceivedBlock:(ASIProgressBlock)aBytesReceivedBlock;
- (void)setBytesSentBlock:(ASIProgressBlock)aBytesSentBlock;
- (void)setDownloadSizeIncrementedBlock:(ASISizeBlock) aDownloadSizeIncrementedBlock;
- (void)setUploadSizeIncrementedBlock:(ASISizeBlock) anUploadSizeIncrementedBlock;
- (void)setDataReceivedBlock:(ASIDataBlock)aReceivedBlock;
- (void)setAuthenticationNeededBlock:(ASIBasicBlock)anAuthenticationBlock;
- (void)setProxyAuthenticationNeededBlock:(ASIBasicBlock)aProxyAuthenticationBlock;
- (void)setRequestRedirectedBlock:(ASIBasicBlock)aRedirectBlock;
typedef void (^ASIBasicBlock)(void);
typedef void (^ASIHeadersBlock)(NSDictionary *responseHeaders);
typedef void (^ASISizeBlock)(long longsize);
typedef void (^ASIProgressBlock)(unsigned long longsize, unsigned long longtotal);
typedef void (^ASIDataBlock)(NSData*data);
获得服务器的响应@property (atomic, assign,readonly) int responseStatusCode;
@property (atomic, retain,readonly) NSString *responseStatusMessage;
@property (atomic, retain) NSDictionary *responseHeaders;
- (NSData *)responseData;
- (NSString *)responseString;
发送POST请求// 1.创建请求
NSURL *url = [NSURL URLWithString:@"http://192.168.1.103:8080/MJServer/login"];
ASIFormDataRequest*request = [ASIFormDataRequest requestWithURL:url];
// 2.设置请求参数
[request addPostValue:@"123" forKey:@"username"];
[request addPostValue:@"123" forKey:@"pwd"];
//注意addPostValue和setPostValue的区别ASIFormDataRequest*request = [ASIFormDataRequest requestWithURL:url];
// 添加普通的请求参数
[request addPostValue:@"MJ" forKey:@"username"];
// 添加文件参数
NSString*file = [[NSBundle mainBundle] pathForResource:@"musicplayer.png" ofType:nil];
[request addFile:file forKey:@"file"];
// 或者
UIImage*image = [UIImage imageNamed:@"musicplayer"];
NSData*data = UIImagePNGRepresentation(image);
[request addData:data withFileName:@"test.png" andContentType:@"image/png" forKey:@"file"];
文件上传 – 添加文件参数- (void)addFile:(NSString *)filePath forKey:(NSString*)key
- (void)addFile:(NSString *)filePath withFileName:(NSString *)fileName andContentType:(NSString *)contentType forKey:(NSString*)key
- (void)addData:(id)datawithFileName:(NSString *)fileName andContentType:(NSString *)contentType forKey:(NSString*)key
文件下载// 设置缓存路径
NSString*caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSString *filepath =[caches stringByAppendingPathComponent:@"test.mp4"];
request.downloadDestinationPath = filepath;
// 设置下载代理
request.downloadProgressDelegate = self.progressView;
// 设置文件的临时路径
request.temporaryFileDownloadPath = tmpFilepath;
// 设置支持断点续传
request.allowResumeForFileDownloads = YES;
监听文件上传\下载进度- (void)setUploadProgressDelegate:(id)newDelegate
- (void)setProgress:(float)newProgress;
缓存ASIDownloadCache*cache = [ASIDownloadCache sharedCache];
- (void)setDefaultCachePolicy:(ASICachePolicy)cachePolicy
- (void)setStoragePath:(NSString*)path
缓存策略 - ASICachePolicy// 设置缓存策略
ASIDownloadCache*cache = [ASIDownloadCache sharedCache];
[cache setDefaultCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy | ASIFallbackToCacheIfLoadFailsCachePolicy];
// 使用缓存
[request setDownloadCache:cache];
// 设置缓存的存储策略(永久存储)
[request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
ASIHTTPRequest缓存的存储策略// 设置缓存策略
ASIDownloadCache*cache = [ASIDownloadCache sharedCache];
[cache setDefaultCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy | ASIFallbackToCacheIfLoadFailsCachePolicy];
// 使用缓存
[ASIHTTPRequest setDefaultCache:cache];
缓存的其他特性[request setSecondsToCache:60 * 60 * 24 * 7]; // 缓存7天
BOOL useCache =[request didUseCachedResponse];
ASIHTTPRequest[ASIHTTPRequest isNetworkInUse];
[ASIHTTPRequest setShouldUpdateNetworkActivityIndicator:YES];
request.shouldContinueWhenAppEntersBackground = YES;
request.numberOfTimesToRetryOnTimeout = 2; // 重试2次
更多推荐
所有评论(0)