我想知道在 HTTP 和 HTTPS 上有相同 API 的情况下,最好的选择(架构)是什么。有没有办法同时支持 HTTP 和 HTTPS 请求,AFHTTPRequestOperationManager
或者我应该有两个子类,一个用于 HTTP 请求,第二个用于 HTTPS 请求?
我觉得baseURL
动态改变不是最好的解决方案
我想知道在 HTTP 和 HTTPS 上有相同 API 的情况下,最好的选择(架构)是什么。有没有办法同时支持 HTTP 和 HTTPS 请求,AFHTTPRequestOperationManager
或者我应该有两个子类,一个用于 HTTP 请求,第二个用于 HTTPS 请求?
我觉得baseURL
动态改变不是最好的解决方案
您可以持有两个 AFHTTPClient* 对象。一个用于 http,另一个用于安全 https。这是基于我的 Requester 类的示例。
-- 请求者.h ---
#import "基础/Foundation.h" #import "AFNetworking.h" 类型定义枚举 { MultipartTypeImageJPEG, MultipartTypeImagePNG, MultipartTypeVideoQuicktime } 多部分类型; 类型定义枚举 { HTTP方法GET, HTTP方法POST, HTTP方法PUT, HTTP方法DELETE } HTTP方法; typedef void (^RequestCallback)(NSError *error, NSInteger statusCode, id json); @interface 请求者:NSObject /** * 单身人士 */ +(实例类型)共享实例; - (void)requestToPath:(NSString *)path 方法:(HTTPMethod)方法 参数:(NSDictionary *)参数 完成:(RequestCallback)回调; - (void)requestMultipartToPath:(NSString *)path 方法:(HTTPMethod)方法 参数:(NSDictionary *)参数 文件数据:(NSData *)文件数据 文件名:(NSString *)文件名 类型:(MultipartType)multipartType 完成:(RequestCallback)回调; - (void)secureRequestToPath:(NSString *)path 方法:(HTTPMethod)方法 参数:(NSDictionary *)参数 完成:(RequestCallback)回调;
-- 请求者.m ---
#import "请求者.h" @接口请求者() @property (nonatomic, strong) AFHTTPClient *httpClient; @property (nonatomic, strong) AFHTTPClient *httpClientSecure; @结尾 @implementation 请求者 + (实例类型)sharedInstance { 静态 id 实例 = nil; 静态 dispatch_once_t 一次令牌; dispatch_once(&onceToken, ^{ instance = [[self alloc] init]; }); 返回实例; } + (void)初始化 { [超级初始化]; NSURL *baseURL = [NSURL URLWithString:YOUR_BASE_ADDRESS_HTTP]; NSURL *secureBaseURL = [NSURL URLWithString:YOUR_BASE_ADDRESS_HTTPS]; [请求者 sharedInstance].httpClient = [AFHTTPClient clientWithBaseURL:baseURL]; [请求者共享实例].httpClient.parameterEncoding = AFJSONParameterEncoding; [请求者 sharedInstance].httpClientSecure = [AFHTTPClient clientWithBaseURL:secureBaseURL]; [请求者共享实例].httpClientSecure.parameterEncoding = AFJSONParameterEncoding; } - (id)初始化 { self = [超级初始化]; 如果(自我){ //... } 回归自我; } #pragma mak - 请求类型 - (NSMutableURLRequest*)requestWithPathPOST:(NSString*)path withParams:(NSDictionary*)params { 参数 = [self addRequiredBodyProperties:params]; NSMutableURLRequest *request = [self.httpClient requestWithMethod:@"POST" path:path parameters:params]; request.cachePolicy = NSURLRequestReloadIgnoringCacheData; request = [self addRequiredHeaderProperties:request]; 退货请求; } - (NSMutableURLRequest*)requestWithPathGET:(NSString*)path withParams:(NSDictionary*)params { 参数 = [self addRequiredBodyProperties:params]; NSMutableURLRequest *request = [self.httpClient requestWithMethod:@"GET" path:path parameters:params]; request.cachePolicy = NSURLRequestReloadIgnoringCacheData; request = [self addRequiredHeaderProperties:request]; 退货请求; } - (NSMutableURLRequest*)requestWithPathPUT:(NSString*)path withParams:(NSDictionary*)params { 参数 = [self addRequiredBodyProperties:params]; NSMutableURLRequest *request = [self.httpClient requestWithMethod:@"PUT" path:path parameters:params]; request.cachePolicy = NSURLRequestReloadIgnoringCacheData; request = [self addRequiredHeaderProperties:request]; 退货请求; } - (NSMutableURLRequest*)requestWithPathDEL:(NSString*)path withParams:(NSDictionary*)params { 参数 = [self addRequiredBodyProperties:params]; NSMutableURLRequest *request = [self.httpClient requestWithMethod:@"DELETE" path:path parameters:params]; request.cachePolicy = NSURLRequestReloadIgnoringCacheData; request = [self addRequiredHeaderProperties:request]; 退货请求; } - (NSMutableURLRequest *)addRequiredHeaderProperties:(NSMutableURLRequest *)request { NSMutableDictionary *dic = [[请求所有HTTPHeaderFields] mutableCopy]; // 这里添加一些每个请求都需要的头部参数 NSString *deviceId = [[[UIDevice currentDevice] identifierForVendor] UUIDString]; [dic setObject:deviceId forKey:@"Device-Id"]; [dic setObject:@"ios" forKey:@"os"]; [请求 setAllHTTPHeaderFields:dic]; 退货请求; } - (NSMutableDictionary *)addRequiredBodyProperties:(NSDictionary *)params { NSMutableDictionary *result = [参数 mutableCopy]; 如果(!结果){ 结果 = [NSMutableDictionary 新]; } // 这里添加一些每个请求都需要的参数 如果(API_KEY){ [结果集对象:API_KEY forKey:@"api_key"]; } 返回结果; } - (NSMutableURLRequest*)requestMultipartWithPath:(NSString*)path method:(NSString *)method withParams:(NSDictionary*)params fileData:(NSData *)fileData fileName:(NSString *)fileName type:(MultipartType)multyPartType { 参数 = [self addRequiredBodyProperties:params]; NSString *mimeType = @""; NSString *name = @"图片"; 开关(多部分类型){ 案例 MultipartTypeImageJPEG: mimeType = @"图像/jpeg"; 如果(文件名) { if ([fileName rangeOfString:@".jpg"].location == NSNotFound && [fileName rangeOfString:@".jpeg"].location == NSNotFound) { 文件名 = [文件名 stringByAppendingString:@".jpg"]; } } 休息; 案例MultipartTypeImagePNG: mimeType = @"图片/png"; 如果(文件名) { if ([fileName rangeOfString:@".png"].location == NSNotFound) { 文件名 = [文件名 stringByAppendingString:@".png"]; } } 休息; 案例MultipartTypeVideoQuicktime: mimeType = @"视频/快速时间"; 名称=@“视频”; 休息; } if (!method || [方法 isEqualToString:@""]) { 方法 = @"POST"; } NSMutableURLRequest *request = [self.httpClient multipartFormRequestWithMethod:方法路径:路径参数:paramsconstructingBodyWithBlock:^(id formData) { [formData appendPartWithFileData:fileData name:name fileName:fileName mimeType:mimeType]; }]; request = [self addRequiredHeaderProperties:request]; 退货请求; } #pragma mark - 全局请求 - (void)JSONRequestOperationWithRequest:(NSMutableURLRequest *)请求回调:(RequestCallback)回调 { [[AFJSONRequestOperation JSONRequestOperationWithRequest:请求成功:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { 如果(回调){ 回调(无,response.statusCode,O); } } 失败:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { NSLog(@"%@",error.description); NSLog(@"%@",JSON); 如果(回调){ 回调(错误,response.statusCode,JSON); } }] 开始]; } #pragma 标记 - - (void)requestToPath:(NSString *)path method:(HTTPMethod)method params:(NSDictionary *)params complete:(RequestCallback)callback { NSMutableURLRequest *request = nil; 开关(方法){ 案例 HTTPMethodGET: request = [self requestWithPathGET:path withParams:params]; 休息; 案例 HTTPMethodPUT: request = [self requestWithPathPUT:path withParams:params]; 休息; 案例 HTTPMethodPOST: request = [self requestWithPathPOST:path withParams:params]; 休息; 案例 HTTPMethodDELETE: request = [self requestWithPathDEL:path withParams:params]; 休息; } [self JSONRequestOperationWithRequest:request callback:callback]; } - (void)requestMultipartToPath:(NSString *)path method:(HTTPMethod)method params:(NSDictionary *)params fileData:(NSData *)fileData fileName:(NSString *)fileName type:(MultipartType)multyPartType complete:(RequestCallback)callback { NSString *methodString = @""; 开关(方法){ 案例 HTTPMethodGET: 方法字符串 = @"GET"; 休息; 案例 HTTPMethodPUT: 方法字符串 = @"PUT"; 休息; 案例 HTTPMethodPOST: 方法字符串 = @"POST"; 休息; 案例 HTTPMethodDELETE: 方法字符串 = @"删除"; 休息; } NSMutableURLRequest *request = [self requestMultipartWithPath:path method:methodString withParams:params fileData:fileData fileName:fileName type:multyPartType]; [self JSONRequestOperationWithRequest:request callback:callback]; } - (void)secureRequestToPath:(NSString *)path method:(HTTPMethod)method params:(NSDictionary *)params complete:(RequestCallback)callback { NSMutableURLRequest *request = nil; 开关(方法){ 案例 HTTPMethodGET: request = [self.httpClientSecure requestWithMethod:@"GET" path:path parameters:params]; 休息; 案例 HTTPMethodPUT: request = [self.httpClientSecure requestWithMethod:@"PUT" path:path parameters:params]; 休息; 案例 HTTPMethodPOST: request = [self.httpClientSecure requestWithMethod:@"POST" path:path parameters:params]; 休息; 案例 HTTPMethodDELETE: request = [self.httpClientSecure requestWithMethod:@"DELETE" path:path parameters:params]; 休息; } AFJSONRequestOperation *oper = [AFJSONRequestOperation JSONRequestOperationWithRequest:request 成功:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { 如果(回调){ 回调(无,response.statusCode,JSON); } } 失败:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { NSLog(@"%@",error.description); NSLog(@"%@",JSON); 如果(回调){ 回调(错误,response.statusCode,JSON); } }]; [操作开始]; } @结尾
用法:
对于 http:
[[请求者 sharedInstance] requestToPath:@"user/authenticate" 方法:HTTPMethodPOST 参数:@{@"username":username,@"password":password} 完成:^(NSError *error, NSInteger statusCode, id json) { 如果(!错误){ //对响应做一些事情 } }];
对于 https:
[[请求者 sharedInstance] secureRequestToPath:@"user/authenticate" 方法:HTTPMethodPOST 参数:@{@"username":username,@"password":password} 完成:^(NSError *error, NSInteger statusCode, id json) { 如果(!错误){ //对响应做一些事情 } }];