ASIDownManager.m 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. //
  2. // ASIDownManager.h
  3. // IBOSS
  4. //
  5. // Created by jiangyh 2017-07-14.
  6. // Copyright (c) 2017年 dongke. All rights reserved.
  7. //
  8. // 功能描述:请求数据对象管理
  9. //
  10. #import "ASIDownManager.h"
  11. #import "ASIFormDataRequest.h"
  12. #import "LeslieImageCache.h"
  13. @interface ASIDownManager()
  14. {
  15. /**
  16. 请求数据对象
  17. */
  18. ASIFormDataRequest *_mRequest;
  19. }
  20. @end
  21. @implementation ASIDownManager
  22. /**
  23. NSOperationQueue对象
  24. */
  25. static NSOperationQueue *queue;
  26. /**
  27. 属性
  28. */
  29. @synthesize delegate;
  30. @synthesize onRequestSuccess;
  31. @synthesize onRequestFail;
  32. @synthesize tag;
  33. @synthesize mWebStr;
  34. /**
  35. 初始化矩阵
  36. @return <#return value description#>
  37. */
  38. - (id)init {
  39. self = [super init];
  40. if (self) {
  41. if(queue){
  42. queue = [[NSOperationQueue alloc]init];
  43. }
  44. }
  45. return self;
  46. }
  47. /**
  48. 请求取消
  49. */
  50. - (void)cancel {
  51. if (_mRequest) {
  52. _mRequest.delegate = nil;
  53. [_mRequest clearDelegatesAndCancel];
  54. [_mRequest release];
  55. _mRequest = nil;
  56. }
  57. }
  58. /**
  59. dealloc
  60. */
  61. - (void)dealloc {
  62. [self cancel];
  63. self.delegate = nil;
  64. self.mWebStr = nil;
  65. [super dealloc];
  66. }
  67. /**
  68. 下载图片
  69. @param imageUrl <#imageUrl description#>
  70. @return <#return value description#>
  71. */
  72. - (NSString *)downloadImageWithURL:(NSString *)imageUrl {
  73. LeslieImageCache *imageCache = [LeslieImageCache sharedCache];
  74. NSString *filePath;
  75. UIImage *image ;
  76. // image = [imageCache getImageFromMemoryForkey:imageUrl];
  77. // // 先从内存中取
  78. // if (image) {
  79. // return image;
  80. // }
  81. // 再从文件中取
  82. filePath = [imageCache getImagePathFromFileForKey:imageUrl];
  83. if (filePath) {
  84. return filePath;
  85. }
  86. image = [self SynchronousDownloadFromURL:imageUrl];
  87. // 先缓存图片到内存
  88. if (image) {
  89. // 再缓存图片到文件系统
  90. NSString *extension = [[imageUrl substringFromIndex:imageUrl.length-3] lowercaseString];
  91. NSString *imageType = nil;
  92. if ([extension isEqualToString:@"jpg"]) {
  93. imageType = [@"jpg" stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]] ;
  94. }else{
  95. imageType = [@"png" stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]] ;
  96. }
  97. filePath= [imageCache writeImageToFile:image forKey:imageUrl ofType:imageType];
  98. return filePath;
  99. }
  100. return nil;
  101. }
  102. /**
  103. 开启同步请求
  104. @param url <#url description#>
  105. @return <#return value description#>
  106. */
  107. - (UIImage *)SynchronousDownloadFromURL:(NSString *)url
  108. {
  109. url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
  110. NSURLRequest *urlReq = [NSURLRequest requestWithURL: [NSURL URLWithString: url]];
  111. NSError *error = nil;
  112. //开启同步请求
  113. NSData *data = [NSURLConnection sendSynchronousRequest:urlReq returningResponse:nil error:&error];
  114. //[_mRequest startSynchronous];
  115. if(error || !data){
  116. return nil;
  117. }
  118. return [[UIImage alloc] initWithData:data];
  119. }
  120. - (void)SynchronousDownloadFileFromURL:(NSString *)url filePath:(NSString*)path
  121. {
  122. url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
  123. NSURLRequest *urlReq = [NSURLRequest requestWithURL: [NSURL URLWithString: url]];
  124. NSError *error = nil;
  125. //开启同步请求
  126. NSData *data = [NSURLConnection sendSynchronousRequest:urlReq returningResponse:nil error:&error];
  127. //[_mRequest startSynchronous];
  128. if(error || !data){
  129. return;
  130. }
  131. if( data )
  132. {
  133. [data writeToFile:path atomically:YES];
  134. }
  135. }
  136. /**
  137. 开启异步请求
  138. @param urlString <#urlString description#>
  139. @param dict <#dict description#>
  140. @param filePath <#filePath description#>
  141. @param filename <#filename description#>
  142. @param fileType <#fileType description#>
  143. @return <#return value description#>
  144. */
  145. - (NSString *)syncPostHttpRequestUrl:(NSString *)urlString
  146. dic:(NSDictionary *)dict
  147. path:(NSString *)filePath
  148. filename:(NSString *)filename
  149. fileType:(NSString *)fileType
  150. {
  151. NSURL *url = [NSURL URLWithString:urlString];
  152. NSString *responseString = nil;
  153. _mRequest = [[ASIFormDataRequest alloc] initWithURL:url];
  154. // 参数
  155. [_mRequest setTimeOutSeconds:120];
  156. _mRequest.useCookiePersistence=YES;
  157. for (NSString *key in dict.allKeys) {
  158. NSString *value = [dict objectForKey:key];
  159. [_mRequest addPostValue:value forKey:key];
  160. }
  161. //文件存在
  162. if(filePath){
  163. if(filename){
  164. [_mRequest setFile:filePath withFileName:filename andContentType:fileType forKey:filename];
  165. }else{
  166. [_mRequest setFile:filePath withFileName:filename andContentType:fileType forKey:@"files"];
  167. }
  168. }
  169. [_mRequest startSynchronous];
  170. NSError* error = [_mRequest error];
  171. if(error || [_mRequest responseStatusCode] != 200){
  172. responseString = @"";
  173. NSLog(@"requestFailed %@",responseString);
  174. return responseString;
  175. }
  176. NSData *data = [_mRequest responseData];
  177. responseString= [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  178. return responseString;
  179. }
  180. /**
  181. 开启异步请求
  182. @param urlString <#urlString description#>
  183. @param dict <#dict description#>
  184. @param filepath filepath description
  185. @param filename filename description
  186. */
  187. - (void)postHttpRequest:(NSString *)urlString
  188. dic:(NSDictionary *)dict
  189. path:(NSString *)filepath
  190. fileName:(NSString *)filename
  191. {
  192. NSURL *url = [NSURL URLWithString:urlString];
  193. _mRequest = [[ASIFormDataRequest alloc] initWithURL:url];
  194. [_mRequest setTimeOutSeconds:120];
  195. for (NSString *key in dict.allKeys) {
  196. NSString *value = [dict objectForKey:key];
  197. [_mRequest addPostValue:value forKey:key];
  198. }
  199. if (filepath) {
  200. [_mRequest addFile:filepath forKey:filename];
  201. }
  202. _mRequest.useCookiePersistence=YES;
  203. [_mRequest setDelegate:self];
  204. [queue addOperation:_mRequest];
  205. [_mRequest startAsynchronous];
  206. }
  207. /**
  208. post异步请求
  209. @param urlString <#urlString description#>
  210. @param dict <#dict description#>
  211. @param files <#files description#>
  212. */
  213. - (void)postHttpRequest:(NSString *)urlString
  214. dic:(NSDictionary *)dict
  215. files:(NSDictionary *)files
  216. {
  217. NSLog(@"PostHttpRequest:%@, %@, %@", urlString, dict, files);
  218. NSURL *url = [NSURL URLWithString:urlString];
  219. _mRequest = [[ASIFormDataRequest alloc] initWithURL:url];
  220. [_mRequest setTimeOutSeconds:120];
  221. /*
  222. 如果设置useCookiePersistence为YES(默认值),cookie会被存储在共享的 NSHTTPCookieStorage 容器中,并且会自动被其他request重用。值得一提的是,ASIHTTPRequest会向服务器发送其他程序创建的cookie(如果这些cookie对特定request有效的话)。
  223. */
  224. //记住用户状态的sessionid
  225. _mRequest.useCookiePersistence=YES;
  226. // 参数值
  227. for (NSString *key in dict.allKeys) {
  228. NSString *value = [dict objectForKey:key];
  229. [_mRequest addPostValue:value forKey:key];
  230. }
  231. // 文件
  232. for (NSString *key in files.allKeys) {
  233. NSString *value = [files objectForKey:key];
  234. [_mRequest addFile:value forKey:key];
  235. }
  236. [_mRequest setDelegate:self];
  237. [queue addOperation:_mRequest];
  238. [_mRequest startAsynchronous];
  239. }
  240. /**
  241. 请求完成函数
  242. @param _request _request description
  243. */
  244. - (void)requestFinished:(ASIHTTPRequest *)_request {
  245. NSData *data = [_request responseData];
  246. NSString *responseStr = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
  247. [self cancel];
  248. // 请求响应状态码不为200
  249. if ([_request responseStatusCode] != 200) {
  250. NSLog(@"requestFinished Invalid StatusCode:%d,%@", [_request responseStatusCode], _request.responseStatusMessage);
  251. // 请求失败函数
  252. if (delegate && onRequestFail) {
  253. [delegate performSelector:onRequestFail withObject:self];
  254. }
  255. return;
  256. }
  257. responseStr = [responseStr stringByReplacingOccurrencesOfString:@"\r" withString:@""];
  258. responseStr = [responseStr stringByReplacingOccurrencesOfString:@"\n" withString:@""];
  259. self.mWebStr = responseStr;
  260. // 请求完成函数
  261. if (delegate && onRequestSuccess) {
  262. [delegate performSelector:onRequestSuccess withObject:self];
  263. }
  264. }
  265. /**
  266. 请求失败函数
  267. @param _request <#_request description#>
  268. */
  269. - (void)requestFailed:(ASIHTTPRequest *)_request {
  270. NSData *data = [_request responseData];
  271. NSString *responseStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] ;
  272. [self cancel];
  273. // 请求完成函数
  274. if (delegate && onRequestFail) {
  275. [delegate performSelector:onRequestFail withObject:self];
  276. }
  277. }
  278. @end