AFHTTPRequestOperationManager

简介: <ol class="syntax highlighted" style="font-family:Menlo,Monaco,Consolas,monospace; line-height:1.5em; font-size:13px; margin:0px!important; padding:0px 0px 0px 5em!important"><li class="line ln1
  1. // AFHTTPRequestOperationManager.m
  2. //
  3. // Copyright (c) 2013-2014 AFNetworking (http://afnetworking.com)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. #import <Foundation/Foundation.h>
  23. #import "AFHTTPRequestOperationManager.h"
  24. #import "AFHTTPRequestOperation.h"
  25. #import <Availability.h>
  26. #import <Security/Security.h>
  27. #if defined(__IPHONE_OS_VERSION_MIN_REQUIRED)
  28. #import <UIKit/UIKit.h>
  29. #endif
  30. @interface AFHTTPRequestOperationManager ()
  31. @property (readwrite, nonatomic, strong) NSURL *baseURL;
  32. @end
  33. @implementation AFHTTPRequestOperationManager
  34. + (instancetype)manager {
  35. return [[self alloc] initWithBaseURL:nil];
  36. }
  37. - (instancetype)init {
  38. return [self initWithBaseURL:nil];
  39. }
  40. - (instancetype)initWithBaseURL:(NSURL *)url {
  41. self = [super init];
  42. if (!self) {
  43. return nil;
  44. }
  45. // Ensure terminal slash for baseURL path, so that NSURL +URLWithString:relativeToURL: works as expected
  46. if ([[url path] length] > 0 && ![[url absoluteString] hasSuffix:@"/"]) {
  47. url = [url URLByAppendingPathComponent:@""];
  48. }
  49. self.baseURL = url;
  50. self.requestSerializer = [AFHTTPRequestSerializer serializer];
  51. self.responseSerializer = [AFJSONResponseSerializer serializer];
  52. self.securityPolicy = [AFSecurityPolicy defaultPolicy];
  53. self.reachabilityManager = [AFNetworkReachabilityManager sharedManager];
  54. self.operationQueue = [[NSOperationQueue alloc] init];
  55. self.shouldUseCredentialStorage = YES;
  56. return self;
  57. }
  58. #pragma mark -
  59. #ifdef _SYSTEMCONFIGURATION_H
  60. #endif
  61. - (void)setRequestSerializer:(AFHTTPRequestSerializer <AFURLRequestSerialization> *)requestSerializer {
  62. NSParameterAssert(requestSerializer);
  63. _requestSerializer = requestSerializer;
  64. }
  65. - (void)setResponseSerializer:(AFHTTPResponseSerializer <AFURLResponseSerialization> *)responseSerializer {
  66. NSParameterAssert(responseSerializer);
  67. _responseSerializer = responseSerializer;
  68. }
  69. #pragma mark -
  70. - (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)request
  71. success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
  72. failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
  73. {
  74. AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
  75. operation.responseSerializer = self.responseSerializer;
  76. operation.shouldUseCredentialStorage = self.shouldUseCredentialStorage;
  77. operation.credential = self.credential;
  78. operation.securityPolicy = self.securityPolicy;
  79. [operation setCompletionBlockWithSuccess:success failure:failure];
  80. return operation;
  81. }
  82. #pragma mark -
  83. - (AFHTTPRequestOperation *)GET:(NSString *)URLString
  84. parameters:(id)parameters
  85. success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
  86. failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
  87. {
  88. NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:@"GET" URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters error:nil];
  89. AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
  90. [self.operationQueue addOperation:operation];
  91. return operation;
  92. }
  93. - (AFHTTPRequestOperation *)HEAD:(NSString *)URLString
  94. parameters:(id)parameters
  95. success:(void (^)(AFHTTPRequestOperation *operation))success
  96. failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
  97. {
  98. NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:@"HEAD" URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters error:nil];
  99. AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *requestOperation, __unused id responseObject) {
  100. if (success) {
  101. success(requestOperation);
  102. }
  103. } failure:failure];
  104. [self.operationQueue addOperation:operation];
  105. return operation;
  106. }
  107. - (AFHTTPRequestOperation *)POST:(NSString *)URLString
  108. parameters:(id)parameters
  109. success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
  110. failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
  111. {
  112. NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:@"POST" URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters error:nil];
  113. AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
  114. [self.operationQueue addOperation:operation];
  115. return operation;
  116. }
  117. - (AFHTTPRequestOperation *)POST:(NSString *)URLString
  118. parameters:(id)parameters
  119. constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block
  120. success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
  121. failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
  122. {
  123. NSMutableURLRequest *request = [self.requestSerializer multipartFormRequestWithMethod:@"POST" URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters constructingBodyWithBlock:block error:nil];
  124. AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
  125. [self.operationQueue addOperation:operation];
  126. return operation;
  127. }
  128. - (AFHTTPRequestOperation *)PUT:(NSString *)URLString
  129. parameters:(id)parameters
  130. success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
  131. failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
  132. {
  133. NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:@"PUT" URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters error:nil];
  134. AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
  135. [self.operationQueue addOperation:operation];
  136. return operation;
  137. }
  138. - (AFHTTPRequestOperation *)PATCH:(NSString *)URLString
  139. parameters:(id)parameters
  140. success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
  141. failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
  142. {
  143. NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:@"PATCH" URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters error:nil];
  144. AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
  145. [self.operationQueue addOperation:operation];
  146. return operation;
  147. }
  148. - (AFHTTPRequestOperation *)DELETE:(NSString *)URLString
  149. parameters:(id)parameters
  150. success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
  151. failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
  152. {
  153. NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:@"DELETE" URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters error:nil];
  154. AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
  155. [self.operationQueue addOperation:operation];
  156. return operation;
  157. }
  158. #pragma mark - NSObject
  159. - (NSString *)description {
  160. return [NSString stringWithFormat:@"<%@: %p, baseURL: %@, operationQueue: %@>", NSStringFromClass([self class]), self, [self.baseURL absoluteString], self.operationQueue];
  161. }
  162. #pragma mark - NSecureCoding
  163. + (BOOL)supportsSecureCoding {
  164. return YES;
  165. }
  166. - (id)initWithCoder:(NSCoder *)decoder {
  167. NSURL *baseURL = [decoder decodeObjectForKey:NSStringFromSelector(@selector(baseURL))];
  168. self = [self initWithBaseURL:baseURL];
  169. if (!self) {
  170. return nil;
  171. }
  172. self.requestSerializer = [decoder decodeObjectOfClass:[AFHTTPRequestSerializer class] forKey:NSStringFromSelector(@selector(requestSerializer))];
  173. self.responseSerializer = [decoder decodeObjectOfClass:[AFHTTPResponseSerializer class] forKey:NSStringFromSelector(@selector(responseSerializer))];
  174. return self;
  175. }
  176. - (void)encodeWithCoder:(NSCoder *)coder {
  177. [coder encodeObject:self.baseURL forKey:NSStringFromSelector(@selector(baseURL))];
  178. [coder encodeObject:self.requestSerializer forKey:NSStringFromSelector(@selector(requestSerializer))];
  179. [coder encodeObject:self.responseSerializer forKey:NSStringFromSelector(@selector(responseSerializer))];
  180. }
  181. #pragma mark - NSCopying
  182. - (id)copyWithZone:(NSZone *)zone {
  183. AFHTTPRequestOperationManager *HTTPClient = [[[self class] allocWithZone:zone] initWithBaseURL:self.baseURL];
  184. HTTPClient.requestSerializer = [self.requestSerializer copyWithZone:zone];
  185. HTTPClient.responseSerializer = [self.responseSerializer copyWithZone:zone];
  186. return HTTPClient;
  187. }
  188. @end
目录
相关文章
|
存储 关系型数据库 MySQL
【MySQL专题】MySQL百万级数据插入效率优化
【MySQL专题】MySQL百万级数据插入效率优化
1256 0
【MySQL专题】MySQL百万级数据插入效率优化
|
资源调度 运维 Java
定时任务报警通知解决方案详解
随着微服务和云计算的兴起,定时任务技术也是发展迅速,不仅能做单机的定时任务,而且在分布式系统下应用也很广泛,成为了业务做兜底、数据处理的第一选择。
2753 3
定时任务报警通知解决方案详解
|
机器学习/深度学习 物联网 大数据
|
数据可视化 图形学
R可视乎|圆环图
对于饼图,上一次学习《R语言数据可视化之美》的时候主要利用graphics包和ggplot包(可见R可视乎|饼图)。这几天的学习中发现还有一个更加简便的方法——ggpie包。接下来做简单描述,然后进入圆环图的学习。
696 0
R可视乎|圆环图
|
关系型数据库 MySQL
MySQL:自动维护create_time和update_time字段
MySQL:自动维护create_time和update_time字段
307 0
|
C++
Qt | 信号和槽的一些总结
学习使用Qt的信号槽机制。
625 0
|
前端开发 数据可视化 数据管理
零起点入门系列教程⑤:用宜搭简单布局一个首页
【零起点入门系列教程】将会带给大家从业务视角出发由浅入深地学习用宜搭实现应用搭建。即便是没有任何代码基础的新手只要跟着系列课程,从0开始慢慢修炼,也能找到成功搭建应用的乐趣。今天第五讲,如何用宜搭简单布局一个首页。
3585 0
零起点入门系列教程⑤:用宜搭简单布局一个首页
|
新零售 供应链 监控
蒙牛集团信息技术助理副总裁、CIO张决:双中台助力蒙牛数字化转型
蒙牛双中台支撑线上线下融合,线上引流线下提货,数据洞察赋能,助力传统销售模式数字化变革。
1116 0
蒙牛集团信息技术助理副总裁、CIO张决:双中台助力蒙牛数字化转型
|
前端开发 JavaScript 数据库
【前端】JavaScript 实现下载图片但不自动预览图片
【前端】JavaScript 实现下载图片但不自动预览图片
1198 0
|
安全 架构师 Java
两个天才黑客:一人在牢狱之灾后退隐江湖,一人蜕变成阿里巴巴“守护神”!
     黑  客   “黑客”这个词, 在许多人印象里虽然不够光彩正派, 但却酷劲十足。 他们在我们的常规认知里,他们, 是一群隐身于网络的计算机大神, 是始终笼罩着一层神秘面纱的人间怪杰。
2049 1

热门文章

最新文章