AFHTTPRequestOperationManager

  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
时间: 2024-12-02 09:47:09

AFHTTPRequestOperationManager的相关文章

网络之AFNetworking

Json.Xml解析第三方库多了去,就不一一说明,现在开始进入AFNetworking.由于AFNetworking支持ARC,ASI不支持ARC,现在越来越多的开始使用AFNetworking. http://blog.csdn.net/huang2009303513/article/details/41039681 用法都是一样的,保存起来防止博客丢失 //在请求响应时,有时候响应的内容可能是text/html.text/plain格式,所以需要在AFURLResponseSerializa

云之讯语音、短信验证码实现

使用云之讯语音验证码功能,需要到云之讯开放平台去注册对应的账号,才能使用. 这里http://www.ucpaas.com/doc/doc_rest3-2.jsp 是官方文档 ,需要自己研究第一行文字,才能 明白业务功能的实现点. 下面是一个封装好的类,针对使用该SDK的帮助类: // // HYBUCSSDKHelper.h // UCSVoiceOrSMSVerifyCodeDemo // // Created by 黄仪标 on 15/2/2. // #import <Foundation

Swift版音乐播放器(简化版)

这几天闲着也是闲着,学习一下Swift的,于是到开源社区Download了个OC版的音乐播放器,练练手,在这里发扬开源精神, 希望对大家有帮助! 这个DEMO里,使用到了 AudioPlayer(对音频封装的库) FreeStreamer(老外写的音频高效处理库) LKDBHelper(将数据模型直接写到数据库中的库) AFNetworking (网络库) SDWebImage (图片获取库) 另外,我也把OC版的ProgressHUD转成了Swift版本的HYBProgressHUD,希望对大

iOS访问https ssl和tls双向加密

文章已经移到此处:http://www.henishuo.com/ios-https-tls-ssl/ 关于https和ssl的原理,请到此处查看:http://blog.163.com/magicc_love/blog/static/185853662201321423527263/ 由于项目需求,访问服务是https的,并且使用的是ssl加密方式 下面说明使用MKNetworkit网络库实现的代码: - (void)testClientCertificate { SecIdentityRef

iOS开发之AFNetworking

1.概述 AFNetworking简称AFN,是iOS开发中主流第三方框架之一.苹果的一套框架NSContention发送请求与接收请求的方式十分繁琐.操作起来很不方便.不仅要做区分各种请求设置各种不同的参数,而且还要经常在多线程里操作,同时还要对请求与返回的数据做各种序列化的操作,同时还要考虑请求数据的安全等一堆问题.而AFNetworking帮我们简化了网络操作. 2.早前的几个网络框架 1.ASI框架: HTTP终结者.很牛, 但是有BUG, 已经停止更新. 2.MKNetworkKit

[翻译] Working with NSURLSession: AFNetworking 2.0

Working with NSURLSession: AFNetworking 2.0   简单翻译,有很多错误,看官无法理解处请英文原文对照. http://code.tutsplus.com/tutorials/working-with-nsurlsession-afnetworking-20--mobile-22651 by Bart Jacobs3 Feb 2014 In the previous installments of this series, we've taken a cl

iOS AFN的再次封装

我用一个单例类将一些常用的网络请求进行了二次封装,主要包括post请求 get请求  图片文件上传下载  视频的断点续传等功能. 首先大家先去github上下载AFN,将文件夹内的AFNetworking文件夹拖入工程中,然后创建一个图片文件参数类代码如下: #import <Foundation/Foundation.h> @interface UploadParam : NSObject /** * 上传文件的二进制数据 */ @property (nonatomic, strong) N

AFNnetworking详解

AFN 一.什么是AFN 全称是AFNetworking,是对NSURLConnection的一层封装 虽然运行效率没有ASI高,但是使用比ASI简单 在iOS开发中,使用比较广泛 AFN的github地址 https://github.com/pokeb/AFNetworking/AFNetworking 二.AFN结构 NSURLConnection AFURLConnectionOperation AFHTTPRequestOperation AFHTTPRequestOperationM

[翻译] AFNetworking 2.0

大名鼎鼎的开源网络库AFNetworking 2.0,目前只是翻译了Github上的链接文章,使用教程请点击 http://www.cnblogs.com/YouXianMing/p/3651462.html   https://github.com/AFNetworking/AFNetworking   AFNetworking is a delightful networking library for iOS and Mac OS X. It's built on top of the F