以下仅将关键代码列出供参与学习使用,在网络请求方面
AFHTTPSessionManager 和AFHTTPRequestOperationManager 两个相比在代码实现方式及请求原理上差不太多(包括在RAC的支持上).
但在代理服务上AFHTTPSessionManager支持很好,AFHTTPRequestOperationManager目前不支持:
以下是AFHTTPSessionManager代理实现方式:
@property(nonatomic,strong)AFHTTPSessionManager *manager; //AF请求对象
#pragma mark httpMode
/**
* 初始化HTTP
*/
- (void)httpInit
{
//获取实例对象
//self.manager = [AFHTTPSessionManager manager];
//设置代理并获取实例对象
self.manager = [[AFHTTPSessionManager manager] initWithBaseURL:nil sessionConfiguration:[self setProxyWithConfig]];
//申明返回的结果是JSON类型
self.manager.responseSerializer = [AFJSONResponseSerializer serializer];
//如果报接受类型不一致请替换一致text/html
self.manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
//清求时间设置
self.manager.requestSerializer.timeoutInterval = 30;
//添加header头信息
[self addRequestHeader];
}
/**
* 代理设置
*/
- (NSURLSessionConfiguration *)setProxyWithConfig
{
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
config.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
config.connectionProxyDictionary = @
{
@"HTTPEnable":@YES,
(id)kCFStreamPropertyHTTPProxyHost:@"127.0.0.1",
(id)kCFStreamPropertyHTTPProxyPort:@80,
@"HTTPSEnable":@YES,
(id)kCFStreamPropertyHTTPSProxyHost:@"127.0.0.1",
(id)kCFStreamPropertyHTTPSProxyPort:@80
};
return config;
}
通过以下方式即可实现代理的请求,至于后续的GET POST UPLOAD DOWNLOAD 自己根据实际情况封装实现一下就可以,
下面是用GET方的请求,同时实现的NSURLCache实现对所请求数据的缓存处理
/**
* 发送getCache请求
*/
- (void)startCache:(NSString *)uri tag:(NSInteger)tag
{
[self httpInit];
//根据url生成cache数据
NSURLRequest *aRequestKey = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",self.baseUrl,uri]]];
NSLog(@"%@",[NSString stringWithFormat:@"%@%@",self.baseUrl,uri]);
//获取并缓存cache
__weak typeof(self)weakSelf = self;
[self.manager GET:[NSString stringWithFormat:@"%@%@",self.baseUrl,[self getUriWithTime:uri]] parameters:nil success:^(NSURLSessionDataTask *task,id responseObject) {
[weakSelf requestFinished:responseObject tag:tag];
//将获取的数据缓存缓存cache数据
if ([weakSelf.errCode intValue] <= 0 && [NSJSONSerialization isValidJSONObject:responseObject]) {
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:responseObject options:NSJSONWritingPrettyPrinted error:nil];
NSCachedURLResponse *cachedURLResponse = [[NSURLCache sharedURLCache] cachedResponseForRequest:aRequestKey];
cachedURLResponse = [[NSCachedURLResponse alloc] initWithResponse:task.response data:jsonData userInfo:nil storagePolicy:NSURLCacheStorageAllowed];
[[NSURLCache sharedURLCache] storeCachedResponse:cachedURLResponse forRequest:aRequestKey];
} else {
[weakSelf requestFailed:tag];
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSCachedURLResponse *cachedResponse = [[NSURLCache sharedURLCache] cachedResponseForRequest:aRequestKey];
if (cachedResponse != nil && [[cachedResponse data] length] > 0) {
NSDictionary *cachedDic = [NSJSONSerialization JSONObjectWithData:[cachedResponse data] options:NSJSONReadingMutableContainers error:nil];
[weakSelf requestFinished:cachedDic tag:tag];
}else{
[weakSelf requestFailed:tag];
}
}];
}