| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330 |
- //
- // ASIDownManager.h
- // IBOSS
- //
- // Created by jiangyh 2017-07-14.
- // Copyright (c) 2017年 dongke. All rights reserved.
- //
- // 功能描述:请求数据对象管理
- //
- #import "ASIDownManager.h"
- #import "ASIFormDataRequest.h"
- #import "LeslieImageCache.h"
- @interface ASIDownManager()
- {
- /**
- 请求数据对象
- */
- ASIFormDataRequest *_mRequest;
- }
- @end
- @implementation ASIDownManager
- /**
- NSOperationQueue对象
- */
- static NSOperationQueue *queue;
- /**
- 属性
- */
- @synthesize delegate;
- @synthesize onRequestSuccess;
- @synthesize onRequestFail;
- @synthesize tag;
- @synthesize mWebStr;
- /**
- 初始化矩阵
-
- @return <#return value description#>
- */
- - (id)init {
- self = [super init];
- if (self) {
- if(queue){
- queue = [[NSOperationQueue alloc]init];
- }
- }
- return self;
- }
- /**
- 请求取消
- */
- - (void)cancel {
- if (_mRequest) {
- _mRequest.delegate = nil;
- [_mRequest clearDelegatesAndCancel];
- [_mRequest release];
- _mRequest = nil;
- }
- }
- /**
- dealloc
- */
- - (void)dealloc {
- [self cancel];
- self.delegate = nil;
- self.mWebStr = nil;
- [super dealloc];
- }
- /**
- 下载图片
-
- @param imageUrl <#imageUrl description#>
- @return <#return value description#>
- */
- - (NSString *)downloadImageWithURL:(NSString *)imageUrl {
- LeslieImageCache *imageCache = [LeslieImageCache sharedCache];
- NSString *filePath;
- UIImage *image ;
- // image = [imageCache getImageFromMemoryForkey:imageUrl];
- // // 先从内存中取
- // if (image) {
- // return image;
- // }
-
- // 再从文件中取
- filePath = [imageCache getImagePathFromFileForKey:imageUrl];
- if (filePath) {
- return filePath;
- }
-
- image = [self SynchronousDownloadFromURL:imageUrl];
- // 先缓存图片到内存
- if (image) {
- // 再缓存图片到文件系统
- NSString *extension = [[imageUrl substringFromIndex:imageUrl.length-3] lowercaseString];
- NSString *imageType = nil;
-
- if ([extension isEqualToString:@"jpg"]) {
- imageType = [@"jpg" stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]] ;
- }else{
- imageType = [@"png" stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]] ;
- }
-
- filePath= [imageCache writeImageToFile:image forKey:imageUrl ofType:imageType];
- return filePath;
- }
- return nil;
- }
- /**
- 开启同步请求
-
- @param url <#url description#>
- @return <#return value description#>
- */
- - (UIImage *)SynchronousDownloadFromURL:(NSString *)url
- {
- url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
- NSURLRequest *urlReq = [NSURLRequest requestWithURL: [NSURL URLWithString: url]];
- NSError *error = nil;
- //开启同步请求
- NSData *data = [NSURLConnection sendSynchronousRequest:urlReq returningResponse:nil error:&error];
- //[_mRequest startSynchronous];
-
- if(error || !data){
- return nil;
- }
-
- return [[UIImage alloc] initWithData:data];
- }
- - (void)SynchronousDownloadFileFromURL:(NSString *)url filePath:(NSString*)path
- {
- url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
- NSURLRequest *urlReq = [NSURLRequest requestWithURL: [NSURL URLWithString: url]];
- NSError *error = nil;
- //开启同步请求
- NSData *data = [NSURLConnection sendSynchronousRequest:urlReq returningResponse:nil error:&error];
- //[_mRequest startSynchronous];
-
- if(error || !data){
- return;
- }
- if( data )
- {
- [data writeToFile:path atomically:YES];
- }
-
- }
- /**
- 开启异步请求
-
- @param urlString <#urlString description#>
- @param dict <#dict description#>
- @param filePath <#filePath description#>
- @param filename <#filename description#>
- @param fileType <#fileType description#>
- @return <#return value description#>
- */
- - (NSString *)syncPostHttpRequestUrl:(NSString *)urlString
- dic:(NSDictionary *)dict
- path:(NSString *)filePath
- filename:(NSString *)filename
- fileType:(NSString *)fileType
- {
- NSURL *url = [NSURL URLWithString:urlString];
- NSString *responseString = nil;
- _mRequest = [[ASIFormDataRequest alloc] initWithURL:url];
- // 参数
- [_mRequest setTimeOutSeconds:120];
- _mRequest.useCookiePersistence=YES;
-
- for (NSString *key in dict.allKeys) {
- NSString *value = [dict objectForKey:key];
- [_mRequest addPostValue:value forKey:key];
- }
-
- //文件存在
- if(filePath){
- if(filename){
- [_mRequest setFile:filePath withFileName:filename andContentType:fileType forKey:filename];
- }else{
- [_mRequest setFile:filePath withFileName:filename andContentType:fileType forKey:@"files"];
- }
-
-
- }
- [_mRequest startSynchronous];
-
- NSError* error = [_mRequest error];
- if(error || [_mRequest responseStatusCode] != 200){
- responseString = @"";
- NSLog(@"requestFailed %@",responseString);
- return responseString;
-
- }
- NSData *data = [_mRequest responseData];
- responseString= [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
-
- return responseString;
-
- }
- /**
- 开启异步请求
-
- @param urlString <#urlString description#>
- @param dict <#dict description#>
- @param filepath filepath description
- @param filename filename description
- */
- - (void)postHttpRequest:(NSString *)urlString
- dic:(NSDictionary *)dict
- path:(NSString *)filepath
- fileName:(NSString *)filename
- {
- NSURL *url = [NSURL URLWithString:urlString];
- _mRequest = [[ASIFormDataRequest alloc] initWithURL:url];
- [_mRequest setTimeOutSeconds:120];
-
- for (NSString *key in dict.allKeys) {
- NSString *value = [dict objectForKey:key];
- [_mRequest addPostValue:value forKey:key];
- }
-
- if (filepath) {
- [_mRequest addFile:filepath forKey:filename];
- }
-
- _mRequest.useCookiePersistence=YES;
- [_mRequest setDelegate:self];
- [queue addOperation:_mRequest];
- [_mRequest startAsynchronous];
- }
- /**
- post异步请求
-
- @param urlString <#urlString description#>
- @param dict <#dict description#>
- @param files <#files description#>
- */
- - (void)postHttpRequest:(NSString *)urlString
- dic:(NSDictionary *)dict
- files:(NSDictionary *)files
- {
- NSLog(@"PostHttpRequest:%@, %@, %@", urlString, dict, files);
- NSURL *url = [NSURL URLWithString:urlString];
- _mRequest = [[ASIFormDataRequest alloc] initWithURL:url];
- [_mRequest setTimeOutSeconds:120];
- /*
- 如果设置useCookiePersistence为YES(默认值),cookie会被存储在共享的 NSHTTPCookieStorage 容器中,并且会自动被其他request重用。值得一提的是,ASIHTTPRequest会向服务器发送其他程序创建的cookie(如果这些cookie对特定request有效的话)。
- */
- //记住用户状态的sessionid
- _mRequest.useCookiePersistence=YES;
- // 参数值
- for (NSString *key in dict.allKeys) {
- NSString *value = [dict objectForKey:key];
- [_mRequest addPostValue:value forKey:key];
- }
- // 文件
- for (NSString *key in files.allKeys) {
- NSString *value = [files objectForKey:key];
- [_mRequest addFile:value forKey:key];
- }
-
- [_mRequest setDelegate:self];
- [queue addOperation:_mRequest];
- [_mRequest startAsynchronous];
- }
- /**
- 请求完成函数
-
- @param _request _request description
- */
- - (void)requestFinished:(ASIHTTPRequest *)_request {
- NSData *data = [_request responseData];
- NSString *responseStr = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
-
- [self cancel];
- // 请求响应状态码不为200
- if ([_request responseStatusCode] != 200) {
- NSLog(@"requestFinished Invalid StatusCode:%d,%@", [_request responseStatusCode], _request.responseStatusMessage);
- // 请求失败函数
- if (delegate && onRequestFail) {
- [delegate performSelector:onRequestFail withObject:self];
- }
- return;
- }
-
- responseStr = [responseStr stringByReplacingOccurrencesOfString:@"\r" withString:@""];
- responseStr = [responseStr stringByReplacingOccurrencesOfString:@"\n" withString:@""];
- self.mWebStr = responseStr;
-
- // 请求完成函数
- if (delegate && onRequestSuccess) {
- [delegate performSelector:onRequestSuccess withObject:self];
- }
- }
- /**
- 请求失败函数
-
- @param _request <#_request description#>
- */
- - (void)requestFailed:(ASIHTTPRequest *)_request {
- NSData *data = [_request responseData];
- NSString *responseStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] ;
-
- [self cancel];
- // 请求完成函数
- if (delegate && onRequestFail) {
- [delegate performSelector:onRequestFail withObject:self];
- }
- }
- @end
|