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
目录
相关文章
WKWebView 加载 http:// *** 报错WebPageProxy::didFailProvisionalLoadForFrame:
WKWebView 加载 http:// *** 报错WebPageProxy::didFailProvisionalLoadForFrame:
2098 0
|
11月前
|
iOS开发
iOS 审核 2.1 被拒解决办法
iOS 审核 2.1 被拒解决办法
578 0
|
机器学习/深度学习 并行计算 算法
如何在 iOS 工程中使用 OpenCV
如何在 iOS 工程中使用 OpenCV
如何在 iOS 工程中使用 OpenCV
|
4天前
|
弹性计算 安全 API
访问控制(RAM)|云上安全使用AccessKey的最佳实践
集中管控AK/SK的生命周期,可以极大降低AK/SK管理和使用成本,同时通过加密和轮转的方式,保证AK/SK的安全使用,本次分享为您介绍产品原理,以及具体的使用步骤。
101781 0
|
4天前
|
SQL 关系型数据库 分布式数据库
Doodle Jump — 使用Flutter&Flame开发游戏真不错!
用Flutter&Flame开发游戏是一种什么体验?最近网上冲浪的时候,我偶然发现了一个国外的游戏网站,类似于国内的4399。在浏览时,我遇到了一款经典的小游戏:Doodle Jump...
|
12天前
|
弹性计算 运维 安全
访问控制(RAM)|云上程序使用临时凭证的最佳实践
STS临时访问凭证是阿里云提供的一种临时访问权限管理服务,通过STS获取可以自定义时效和访问权限的临时身份凭证,减少长期访问密钥(AccessKey)泄露的风险。本文将为您介绍产品原理,以及具体的使用步骤。
151033 4
|
10天前
|
数据采集 存储 运维
提升团队工程交付能力,从“看见”工程活动和研发模式开始
本文从统一工程交付的概念模型开始,介绍了如何将应用交付的模式显式地定义出来,并通过工具平台落地。
119990 57
|
10天前
|
监控 负载均衡 Java
深入探究Java微服务架构:Spring Cloud概论
**摘要:** 本文深入探讨了Java微服务架构中的Spring Cloud,解释了微服务架构如何解决传统单体架构的局限性,如松耦合、独立部署、可伸缩性和容错性。Spring Cloud作为一个基于Spring Boot的开源框架,提供了服务注册与发现、负载均衡、断路器、配置中心、API网关等组件,简化了微服务的开发、部署和管理。文章详细介绍了Spring Cloud的核心模块,如Eureka、Ribbon、Hystrix、Config、Zuul和Sleuth,并通过一个电商微服务系统的实战案例展示了如何使用Spring Cloud构建微服务应用。
103499 8
|
12天前
|
人工智能 Serverless 对象存储
让你的文档从静态展示到一键部署可操作验证
通过函数计算的能力让阿里云的文档从静态展示升级为动态可操作验证,用户在文档中单击一键部署可快速完成代码的部署及测试。这一改变已在函数计算的活动沙龙中得到用户的认可。
120826 215