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:
2212 0
|
iOS开发
iOS 审核 2.1 被拒解决办法
iOS 审核 2.1 被拒解决办法
617 0
|
机器学习/深度学习 并行计算 算法
如何在 iOS 工程中使用 OpenCV
如何在 iOS 工程中使用 OpenCV
如何在 iOS 工程中使用 OpenCV
|
11天前
|
SQL 容灾 关系型数据库
[版本更新] PolarDB-X V2.4 列存引擎开源正式发布
[版本更新] PolarDB-X V2.4 列存引擎开源正式发布!
[版本更新] PolarDB-X V2.4 列存引擎开源正式发布
|
11天前
|
存储 关系型数据库 分布式数据库
数据管理的艺术:PolarDB开源版详评与实战部署策略(二)
PolarDB-PG是阿里云的一款云原生关系型数据库,100%兼容PostgreSQL,支持Oracle语法,采用Shared-Storage存储计算分离架构,提供极致弹性、毫秒级延迟的HTAP能力。具备高可用、高可靠和弹性扩展特性,支持单机、存储计算分离和X-Paxos三节点等多种部署形态。通过Docker可快速部署实例,包括单节点、一主一备和HTAP(一主两备)实例。此外,文章还介绍了在ECS上使用ESSD云盘搭建PolarDB-PG的详细步骤,适合开发和测试环境。
178103 20
|
10天前
|
人工智能 自然语言处理 安全
通义千问 2.5 “客串” ChatGPT4,你分的清吗?
这篇文章介绍了使用开源工具NextChat和Higress搭建的一个模拟ChatGPT和通义千问对话PK的测试场景。
87647 7
|
11天前
|
存储 关系型数据库 MySQL
数据管理的艺术:PolarDB开源版详评与实战部署策略(一)
PolarDB-X是阿里巴巴自研的高性能云原生分布式数据库,基于共享存储的Shared-nothing架构,支持MySQL生态,具备金融级高可用、分布式水平扩展、HTAP混合负载等能力。它通过CN(计算节点)和DN(存储节点)实现计算与存储分离,保证数据强一致性,并支持全局二级索引和多主多写。PolarDB-X开源版提供更高程度的定制化和控制权,适合追求技术自主性和成本优化的开发者。部署方式包括RPM包、PXD工具和Kubernetes,其中PXD工具提供了一键部署的便利性。
180357 19
|
10天前
|
SQL 存储 调度
从 Volcano 火山模型到 Pipeline 执行模型,阿里云数据库 SelectDB 内核 Apache Doris 执行模型的迭代
一个合适的执行模型对于提高查询效率和系统性能至关重要。本文全面剖析 Apache Doris Pipeline 执行模型的设计与改造历程,并在 2.1 版本对并发执行模式与调度模式进一步优化,解决了执行并发受限、执行及调度开销大等问题。
从 Volcano 火山模型到 Pipeline 执行模型,阿里云数据库 SelectDB 内核 Apache Doris 执行模型的迭代
|
11天前
|
Java Linux Go
流水线 YAML 高级用法来了,大幅降低重复代码、灵活编排多任务
云效 Flow 流水线 YAML 引入了 template 语法,支持使用模板语言来动态渲染流水线 YAML,满足多个相同或类似逻辑的 Job 批量配置场景,满足多 Job 按需动态生成场景,帮助降低流水线 YAML 重复代码,灵活编排多任务。
70204 7
|
11天前
|
分布式计算 Java API
Java8 Lambda实现源码解析
Java8的lambda应该大家都比较熟悉了,本文主要从源码层面探讨一下lambda的设计和实现。
162622 12