今天在项目中要封装一个请求头但是用如下方法总是失败:
求其原因不知道:
于是乎改用了属性对象后居然成功了。。:
//
// RequestManager.m
// 获取天气demo
//
// Created by ios on 16/9/13.
// Copyright © 2016年 ios. All rights reserved.
//
#import "RequestManager.h"
#import "AFNetworking/AFNetworking.h"
@interface RequestManager()
@property (nonatomic,strong) AFHTTPSessionManager *manager;
@end
@implementation RequestManager
static id _manager=nil;
// 单例
static id shareInstace=nil;
+(RequestManager *)shareInstasnce{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
shareInstace=[[self alloc] init];
});
return shareInstace;
}
- (instancetype)init
{
self = [super init];
if (self) {
AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];
AFSecurityPolicy *securityPolicy = [AFSecurityPolicy defaultPolicy];
securityPolicy.allowInvalidCertificates = YES;
securityPolicy.validatesDomainName = NO;
manager.securityPolicy=securityPolicy;
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html", @"text/plain", @"text/json", @"text/javascript", nil];
_manager=manager;
}
return self;
}
-(void)GET:(NSString *)url params:(NSMutableDictionary *)params success:(void (^)(id))success failure:(void (^)(NSError *))failure{
NSLog(@"%@",_manager.requestSerializer.HTTPRequestHeaders);
[_manager GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
success(responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
failure(error);
}];
}
-(void)setHttpRequestheader:(NSDictionary *)headerFieldValueDictionary{
_manager.requestSerializer.timeoutInterval=10.0f;
if (headerFieldValueDictionary != nil) {
for (id httpHeaderField in headerFieldValueDictionary.allKeys) {
id value = headerFieldValueDictionary[httpHeaderField];
if ([httpHeaderField isKindOfClass:[NSString class]] && [value isKindOfClass:[NSString class]]) {
[_manager.requestSerializer setValue:(NSString *)value forHTTPHeaderField:(NSString *)httpHeaderField];//这种情况设置就成功了。。
} else {
NSLog(@"Error, class of key/value in headerFieldValueDictionary should be NSString.");
}
}
}
}
@end
AFNetworking的坑真不少。。