未设置NSURLSessionConfiguration HTTPAdditionalHeaders

时间:2014-07-15 06:56:20

标签: ios nsurlrequest foundation nsurlsession nsurlsessionconfiguration

授权标头在NSURLSessionConfiguration中设置,但未附加到NSURLSessionDataTask。这是Foundation framework中的错误吗?

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
[configuration setHTTPAdditionalHeaders:@{@"Authorization":@"123"}];

// Initialize session with NSURLSessionConfiguration
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
NSURLSessionDataTask *sessionTask = [session dataTaskWithRequest:request
                                        completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
}];

[sessionTask resume];

2 个答案:

答案 0 :(得分:5)

在NSURLSessionConfiguration document中,

  

NSURLSession对象旨在处理的各个方面   适合您的HTTP协议。因此,你不应该修改   以下标题

     

<强>授权

     

连接

     

主机

     

WWW验证

答案 1 :(得分:1)

我在Swift中尝试这个并且它可以工作

    var sessionConfig = NSURLSessionConfiguration.defaultSessionConfiguration()
    var xHTTPAdditionalHeaders: [NSObject : AnyObject] = ["X-test":"taly"]
    sessionConfig.HTTPAdditionalHeaders = xHTTPAdditionalHeaders
    let session = NSURLSession(configuration: sessionConfig)

    let task = session.dataTaskWithURL(url, completionHandler: {data, response, error -> Void in