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
目录
相关文章
《低代码开发师(初级)实战教程》电子版地址下载
本书基于“低代码开发师(初级)认证”的课程内容,对应课程中请假申请、招聘管理等场景,提供了详细的实践指导,让低代码的初学者快速掌握0代码搭建应用的技能。
89 0
《低代码开发师(初级)实战教程》电子版地址下载
|
网络协议 编译器 测试技术
继承的讨论(2)|学习笔记
快速学习继承的讨论(2)
|
16天前
|
存储 人工智能 弹性计算
阿里云弹性计算_加速计算专场精华概览 | 2024云栖大会回顾
2024年9月19-21日,2024云栖大会在杭州云栖小镇举行,阿里云智能集团资深技术专家、异构计算产品技术负责人王超等多位产品、技术专家,共同带来了题为《AI Infra的前沿技术与应用实践》的专场session。本次专场重点介绍了阿里云AI Infra 产品架构与技术能力,及用户如何使用阿里云灵骏产品进行AI大模型开发、训练和应用。围绕当下大模型训练和推理的技术难点,专家们分享了如何在阿里云上实现稳定、高效、经济的大模型训练,并通过多个客户案例展示了云上大模型训练的显著优势。
|
20天前
|
存储 人工智能 调度
阿里云吴结生:高性能计算持续创新,响应数据+AI时代的多元化负载需求
在数字化转型的大潮中,每家公司都在积极探索如何利用数据驱动业务增长,而AI技术的快速发展更是加速了这一进程。
|
11天前
|
并行计算 前端开发 物联网
全网首发!真·从0到1!万字长文带你入门Qwen2.5-Coder——介绍、体验、本地部署及简单微调
2024年11月12日,阿里云通义大模型团队正式开源通义千问代码模型全系列,包括6款Qwen2.5-Coder模型,每个规模包含Base和Instruct两个版本。其中32B尺寸的旗舰代码模型在多项基准评测中取得开源最佳成绩,成为全球最强开源代码模型,多项关键能力超越GPT-4o。Qwen2.5-Coder具备强大、多样和实用等优点,通过持续训练,结合源代码、文本代码混合数据及合成数据,显著提升了代码生成、推理和修复等核心任务的性能。此外,该模型还支持多种编程语言,并在人类偏好对齐方面表现出色。本文为周周的奇妙编程原创,阿里云社区首发,未经同意不得转载。
|
9天前
|
人工智能 自然语言处理 前端开发
什么?!通义千问也可以在线开发应用了?!
阿里巴巴推出的通义千问,是一个超大规模语言模型,旨在高效处理信息和生成创意内容。它不仅能在创意文案、办公助理、学习助手等领域提供丰富交互体验,还支持定制化解决方案。近日,通义千问推出代码模式,基于Qwen2.5-Coder模型,用户即使不懂编程也能用自然语言生成应用,如个人简历、2048小游戏等。该模式通过预置模板和灵活的自定义选项,极大简化了应用开发过程,助力用户快速实现创意。
|
23天前
|
缓存 监控 Linux
Python 实时获取Linux服务器信息
Python 实时获取Linux服务器信息
|
5天前
|
人工智能 自然语言处理 前端开发
100个降噪蓝牙耳机免费领,用通义灵码从 0 开始打造一个完整APP
打开手机,录制下你完成的代码效果,发布到你的社交媒体,前 100 个@玺哥超Carry、@通义灵码的粉丝,可以免费获得一个降噪蓝牙耳机。
1068 8