如何将自定义HTTP标头添加到gzip压缩请求

时间:2017-01-17 17:25:47

标签: ios objective-c xcode gzip afnetworking-3

我尝试使用自定义HTTP标头发送帖子请求(我添加了Cookie),这是我的代码:

+ (void)performPostRequestToUrl:(NSString *)url
                 Parameters:(NSDictionary *)parameters
               CustomHeader:(NSDictionary *)customHeader
                    Success:(SuccessBlock)success
                    Failure:(FailureBlock) failure{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.requestSerializer = [AFgzipRequestSerializer serializerWithSerializer:[AFJSONRequestSerializer serializer]];

if (customHeader != nil) {
    [manager.requestSerializer setValue:[customHeader objectForKey:@"value"] forHTTPHeaderField:[customHeader objectForKey:@"key"]];
}

[manager POST:url parameters:parameters constructingBodyWithBlock:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    NSDictionary * responseDicionary = responseObject;
    NSHTTPURLResponse *responseHeader = (NSHTTPURLResponse *)task.response;
    success(responseHeader, responseDicionary);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    //[Flurry logError:@"HTTP POST error" message:error.localizedDescription error:error];
    NSString * message = error.localizedDescription;
    BOOL isTimeOut = NO;
    if(error.code == -1009 || error.code == -1004) {
        message = @"No internet connection!\n Connect to internet and try again";
    }else if (error.code == 3840) {
        message = @"An Error Occurred, Please Try Again Later";
    }
    if(error.code == NSURLErrorTimedOut) {
        isTimeOut = YES;
    }
    failure(message, isTimeOut);
    }];
}

上一代码返回此错误

  

错误域= NSURLErrorDomain代码= -999"已取消" UserInfo = {NSErrorFailingURLKey = http://myDomain/api/v1/logout,NSLocalizedDescription =已取消,NSErrorFailingURLStringKey = http://myDomain/api/v1/logout}

如果我评论这一行

manager.requestSerializer = [AFgzipRequestSerializer serializerWithSerializer:[AFJSONRequestSerializer serializer]];

守则很棒!请注意,我需要对我的请求进行gzip压缩:)

0 个答案:

没有答案