Objective c 具有HTTP和HTTPS基本url的AFHTTPRequestOperationManager

Objective c 具有HTTP和HTTPS基本url的AFHTTPRequestOperationManager,objective-c,cocoa-touch,afnetworking,afhttprequestoperation,Objective C,Cocoa Touch,Afnetworking,Afhttprequestoperation,我想知道在HTTP和HTTPS上有相同API的情况下,什么是最好的选择(架构)。有没有一种方法可以在相同的AFHTTPRequestOperationManager中支持HTTP和HTTPS请求,或者我应该有两个子类,一个用于HTTP请求,另一个用于HTTPS请求 我觉得动态更改baseURL不是最好的解决方案您可以保存两个AFHTTPClient*对象。一个用于http,另一个用于安全https。 下面是基于我的请求者类的示例 --请求者--- #导入“Foundation/Foundatio

我想知道在HTTP和HTTPS上有相同API的情况下,什么是最好的选择(架构)。有没有一种方法可以在相同的
AFHTTPRequestOperationManager
中支持HTTP和HTTPS请求,或者我应该有两个子类,一个用于HTTP请求,另一个用于HTTPS请求


我觉得动态更改
baseURL
不是最好的解决方案

您可以保存两个AFHTTPClient*对象。一个用于http,另一个用于安全https。 下面是基于我的请求者类的示例

--请求者---

#导入“Foundation/Foundation.h” #导入“AFNetworking.h” 类型定义枚举 { MultipartTypeImageJPEG, MultipartTypeImagePNG, MultipartTypeVideoQuicktime }多部件类型; 类型定义枚举 { HTTPMethodGET, HTTPMethodPOST, HTTPMethodPUT, HTTPMethodDELETE }HTTPMethod; typedef void(^RequestCallback)(n错误*错误,NSInteger状态码,id json); @接口请求者:NSObject /** *独生子女 */ +(instancetype)sharedInstance; -(void)requestToPath:(NSString*)路径 方法:(HTTPMethod)法 参数:(NSDictionary*)参数 完成:(RequestCallback)回调; -(void)requestMultipartToPath:(NSString*)路径 方法:(HTTPMethod)法 参数:(NSDictionary*)参数 fileData:(NSData*)fileData 文件名:(NSString*)文件名 类型:(MultipartType)multyPartType 完成:(RequestCallback)回调; -(void)secureRequestToPath:(NSString*)路径 方法:(HTTPMethod)法 参数:(NSDictionary*)参数 完成:(RequestCallback)回调; --请求者---

#导入“Requester.h” @接口请求程序() @属性(非原子,强)AFHTTPClient*httpClient; @属性(非原子,强)AFHTTPClient*httpClientSecure; @结束 @实现请求者 +(instancetype)sharedInstance { 静态id实例=nil; 静态调度一次; 一次发送(一次发送)^{ 实例=[[self alloc]init]; }); 返回实例; } +(无效)初始化 { [超级初始化]; NSURL*baseURL=[NSURL URLWithString:YOUR_BASE_ADDRESS_HTTP]; NSURL*secureBaseURL=[NSURL URLWithString:您的基本地址\u HTTPS]; [Requester sharedInstance].httpClient=[AFHTTPClient客户端WithBaseURL:baseURL]; [Requester sharedInstance].httpClient.ParameterEncode=AFJSONParameterEncode; [Requester sharedInstance].httpClientSecure=[AFHTTPClient-clientWithBaseURL:secureBaseURL]; [Requester sharedInstance].httpClientSecure.ParameterEncode=AFJSONParameterEncode; } -(id)init { self=[super init]; 如果(自我){ //... } 回归自我; } #pragma mak-请求类型 -(NSMutableURLRequest*)requestWithPathPOST:(NSString*)路径withParams:(NSDictionary*)参数 { params=[self addRequiredBodyProperties:params]; NSMutableURLRequest*request=[self.httpClient requestWithMethod:@“POST”路径:路径参数:参数]; request.cachePolicy=NSURLRequestReloadIgnoringCacheData; 请求=[self-addRequiredHeaderProperties:request]; 返回请求; } -(NSMutableURLRequest*)requestWithPathGET:(NSString*)path withParams:(NSDictionary*)params { params=[self addRequiredBodyProperties:params]; NSMutableURLRequest*request=[self.httpClient requestWithMethod:@“GET”路径:路径参数:参数]; request.cachePolicy=NSURLRequestReloadIgnoringCacheData; 请求=[self-addRequiredHeaderProperties:request]; 返回请求; } -(NSMutableURLRequest*)requestWithPathPUT:(NSString*)path withParams:(NSDictionary*)params { params=[self addRequiredBodyProperties:params]; NSMutableURLRequest*request=[self.httpClient requestWithMethod:@“PUT”路径:路径参数:参数]; request.cachePolicy=NSURLRequestReloadIgnoringCacheData; 请求=[self-addRequiredHeaderProperties:request]; 返回请求; } -(NSMutableURLRequest*)requestWithPathDEL:(NSString*)路径withParams:(NSDictionary*)参数 { params=[self addRequiredBodyProperties:params]; NSMutableURLRequest*request=[self.httpClient requestWithMethod:@“DELETE”路径:路径参数:参数]; request.cachePolicy=NSURLRequestReloadIgnoringCacheData; 请求=[self-addRequiredHeaderProperties:request]; 返回请求; } -(NSMutableURLRequest*)addRequiredHeaderProperties:(NSMutableURLRequest*)请求 { NSMutableDictionary*dic=[[请求所有HttpHeaderFields]mutableCopy]; //这里添加了每个请求所需的一些头参数 NSString*deviceId=[[[UIDevice currentDevice]identifierForVendor]UUIString]; [dic setObject:deviceId forKey:@“设备Id”]; [dic setObject:@“ios”forKey:@“os”]; [请求设置所有HttpHeaderFields:dic]; 返回请求; } -(NSMutableDictionary*)addRequiredBodyProperties:(NSDictionary*)参数 { NSMutableDictionary*result=[params mutableCopy]; 如果(!结果){ 结果=[NSMutableDictionary new]; } //这里添加了每个请求所需的一些参数 如果(API_键){ [结果集对象:API_KEY forKey:@“API_KEY”]; } 返回结果; } -(NSMutableURLRequest*)requestMultipartWithPath:(NSString*)路径方法:(NSString*)带参数的方法:(NSDictionary*)参数文件数据:(NSData*)文件数据文件名:(NSString*)文件名类型:(MultipartType)multyPartType { params=[self addRequiredBodyProperties:params]; NSString*mimeType=@; NSString*名称=@“图片”; 开关(多部件型){ 大小写MultipartTypeImageJPEG: mimeType=@“图像/jpeg”; 如果(文件名) { 如果([fileName rangeOfString:@.jpg”].location==NSNotFound&[fileName rangeOfString:@.jpeg”].location==NSNotFound) { 文件名=[fi #import "Foundation/Foundation.h" #import "AFNetworking.h" typedef enum { MultipartTypeImageJPEG, MultipartTypeImagePNG, MultipartTypeVideoQuicktime } MultipartType; typedef enum { HTTPMethodGET, HTTPMethodPOST, HTTPMethodPUT, HTTPMethodDELETE } HTTPMethod; typedef void (^RequestCallback)(NSError *error, NSInteger statusCode, id json); @interface Requester : NSObject /** * Singleton */ + (instancetype)sharedInstance; - (void)requestToPath:(NSString *)path method:(HTTPMethod)method params:(NSDictionary *)params complete:(RequestCallback)callback; - (void)requestMultipartToPath:(NSString *)path method:(HTTPMethod)method params:(NSDictionary *)params fileData:(NSData *)fileData fileName:(NSString *)fileName type:(MultipartType)multyPartType complete:(RequestCallback)callback; - (void)secureRequestToPath:(NSString *)path method:(HTTPMethod)method params:(NSDictionary *)params complete:(RequestCallback)callback; #import "Requester.h" @interface Requester() @property (nonatomic, strong) AFHTTPClient *httpClient; @property (nonatomic, strong) AFHTTPClient *httpClientSecure; @end @implementation Requester + (instancetype)sharedInstance { static id instance = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ instance = [[self alloc] init]; }); return instance; } + (void)initialize { [super initialize]; NSURL *baseURL = [NSURL URLWithString:YOUR_BASE_ADDRESS_HTTP]; NSURL *secureBaseURL = [NSURL URLWithString:YOUR_BASE_ADDRESS_HTTPS]; [Requester sharedInstance].httpClient = [AFHTTPClient clientWithBaseURL:baseURL]; [Requester sharedInstance].httpClient.parameterEncoding = AFJSONParameterEncoding; [Requester sharedInstance].httpClientSecure = [AFHTTPClient clientWithBaseURL:secureBaseURL]; [Requester sharedInstance].httpClientSecure.parameterEncoding = AFJSONParameterEncoding; } - (id)init { self = [super init]; if (self) { //... } return self; } #pragma mak - Request Types - (NSMutableURLRequest*)requestWithPathPOST:(NSString*)path withParams:(NSDictionary*)params { params = [self addRequiredBodyProperties:params]; NSMutableURLRequest *request = [self.httpClient requestWithMethod:@"POST" path:path parameters:params]; request.cachePolicy = NSURLRequestReloadIgnoringCacheData; request = [self addRequiredHeaderProperties:request]; return request; } - (NSMutableURLRequest*)requestWithPathGET:(NSString*)path withParams:(NSDictionary*)params { params = [self addRequiredBodyProperties:params]; NSMutableURLRequest *request = [self.httpClient requestWithMethod:@"GET" path:path parameters:params]; request.cachePolicy = NSURLRequestReloadIgnoringCacheData; request = [self addRequiredHeaderProperties:request]; return request; } - (NSMutableURLRequest*)requestWithPathPUT:(NSString*)path withParams:(NSDictionary*)params { params = [self addRequiredBodyProperties:params]; NSMutableURLRequest *request = [self.httpClient requestWithMethod:@"PUT" path:path parameters:params]; request.cachePolicy = NSURLRequestReloadIgnoringCacheData; request = [self addRequiredHeaderProperties:request]; return request; } - (NSMutableURLRequest*)requestWithPathDEL:(NSString*)path withParams:(NSDictionary*)params { params = [self addRequiredBodyProperties:params]; NSMutableURLRequest *request = [self.httpClient requestWithMethod:@"DELETE" path:path parameters:params]; request.cachePolicy = NSURLRequestReloadIgnoringCacheData; request = [self addRequiredHeaderProperties:request]; return request; } - (NSMutableURLRequest *)addRequiredHeaderProperties:(NSMutableURLRequest *)request { NSMutableDictionary *dic = [[request allHTTPHeaderFields] mutableCopy]; // Here adding some header parameters that required on every request NSString *deviceId = [[[UIDevice currentDevice] identifierForVendor] UUIDString]; [dic setObject:deviceId forKey:@"Device-Id"]; [dic setObject:@"ios" forKey:@"os"]; [request setAllHTTPHeaderFields:dic]; return request; } - (NSMutableDictionary *)addRequiredBodyProperties:(NSDictionary *)params { NSMutableDictionary *result = [params mutableCopy]; if (!result) { result = [NSMutableDictionary new]; } // Here adding some parameters that required on every request if (API_KEY) { [result setObject:API_KEY forKey:@"api_key"]; } return result; } - (NSMutableURLRequest*)requestMultipartWithPath:(NSString*)path method:(NSString *)method withParams:(NSDictionary*)params fileData:(NSData *)fileData fileName:(NSString *)fileName type:(MultipartType)multyPartType { params = [self addRequiredBodyProperties:params]; NSString *mimeType = @""; NSString *name = @"picture"; switch (multyPartType) { case MultipartTypeImageJPEG: mimeType = @"image/jpeg"; if (fileName) { if ([fileName rangeOfString:@".jpg"].location == NSNotFound && [fileName rangeOfString:@".jpeg"].location == NSNotFound) { fileName = [fileName stringByAppendingString:@".jpg"]; } } break; case MultipartTypeImagePNG: mimeType = @"image/png"; if (fileName) { if ([fileName rangeOfString:@".png"].location == NSNotFound) { fileName = [fileName stringByAppendingString:@".png"]; } } break; case MultipartTypeVideoQuicktime: mimeType = @"video/quicktime"; name = @"video"; break; } if (!method || [method isEqualToString:@""]) { method = @"POST"; } NSMutableURLRequest *request = [self.httpClient multipartFormRequestWithMethod:method path:path parameters:params constructingBodyWithBlock:^(id formData) { [formData appendPartWithFileData:fileData name:name fileName:fileName mimeType:mimeType]; }]; request = [self addRequiredHeaderProperties:request]; return request; } #pragma mark - Global Requests - (void)JSONRequestOperationWithRequest:(NSMutableURLRequest *)request callback:(RequestCallback)callback { [[AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { if (callback) { callback(nil,response.statusCode,O); } } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { NSLog(@"%@",error.description); NSLog(@"%@",JSON); if (callback) { callback(error,response.statusCode,JSON); } }] start]; } #pragma mark - - (void)requestToPath:(NSString *)path method:(HTTPMethod)method params:(NSDictionary *)params complete:(RequestCallback)callback { NSMutableURLRequest *request = nil; switch (method) { case HTTPMethodGET: request = [self requestWithPathGET:path withParams:params]; break; case HTTPMethodPUT: request = [self requestWithPathPUT:path withParams:params]; break; case HTTPMethodPOST: request = [self requestWithPathPOST:path withParams:params]; break; case HTTPMethodDELETE: request = [self requestWithPathDEL:path withParams:params]; break; } [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 = @""; switch (method) { case HTTPMethodGET: methodString = @"GET"; break; case HTTPMethodPUT: methodString = @"PUT"; break; case HTTPMethodPOST: methodString = @"POST"; break; case HTTPMethodDELETE: methodString = @"DELETE"; break; } 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; switch (method) { case HTTPMethodGET: request = [self.httpClientSecure requestWithMethod:@"GET" path:path parameters:params]; break; case HTTPMethodPUT: request = [self.httpClientSecure requestWithMethod:@"PUT" path:path parameters:params]; break; case HTTPMethodPOST: request = [self.httpClientSecure requestWithMethod:@"POST" path:path parameters:params]; break; case HTTPMethodDELETE: request = [self.httpClientSecure requestWithMethod:@"DELETE" path:path parameters:params]; break; } AFJSONRequestOperation *oper = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { if (callback) { callback(nil,response.statusCode,JSON); } } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { NSLog(@"%@",error.description); NSLog(@"%@",JSON); if (callback) { callback(error,response.statusCode,JSON); } }]; [oper start]; } @end [[Requester sharedInstance] requestToPath:@"user/authenticate" method:HTTPMethodPOST params:@{@"username":username,@"password":password} complete:^(NSError *error, NSInteger statusCode, id json) { if (!error) { //do something with response } }]; [[Requester sharedInstance] secureRequestToPath:@"user/authenticate" method:HTTPMethodPOST params:@{@"username":username,@"password":password} complete:^(NSError *error, NSInteger statusCode, id json) { if (!error) { //do something with response } }];