美文网首页
AFNetworking3.0添加请求头Header

AFNetworking3.0添加请求头Header

作者: 禾子_____ | 来源:发表于2016-05-09 10:08 被阅读6525次
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
    
    //不设置会报-1016或者会有编码问题
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    //不设置会报 error 3840
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    
    //创建你得请求url、设置请求头
    
    NSString *urlString = [NSString stringWithFormat:@"http://xxxxx"];
    
    NSMutableURLRequest *request = [[AFJSONRequestSerializer serializer] requestWithMethod:@"POST" URLString:urlString parameters:你的参数 error:nil];
    
    [request addValue:你需要的accept-id forHTTPHeaderField:@"Accept-Id"];
    
    [request addValue:你需要的user-agent forHTTPHeaderField:@"User-Agent"];
    
    NSData *body = 你需要提交的data;
    
    [request setHTTPBody:body];
    
    //发起请求
    
    [[manager dataTaskWithRequest:request completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
    
    if (!error) {
    
    NSLog(@"Reply JSON: %@", responseObject);
    
    if ([responseObject isKindOfClass:[NSDictionary class]]) {
    
    //处理你的数据
    
    }
    
    } else {
    
    NSLog(@"Error: %@, %@, %@", error, response, responseObject);
    
    }
    
    }] resume]; 
    
    

    相关文章

      网友评论

          本文标题:AFNetworking3.0添加请求头Header

          本文链接:https://www.haomeiwen.com/subject/vmqerttx.html