NSURLSessionConfiguration HTTPAdditionalHeaders not set

一曲冷凌霜 提交于 2019-12-22 05:52:51

问题


Authorization header is set in NSURLSessionConfiguration, however it is not attached to NSURLSessionDataTask. Is this a bug in 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];

回答1:


I try this in Swift and it works

    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



回答2:


In the NSURLSessionConfiguration document,

An NSURLSession object is designed to handle various aspects of the HTTP protocol for you. As a result, you should not modify the following headers:

Authorization

Connection

Host

WWW-Authenticate



来源:https://stackoverflow.com/questions/24751768/nsurlsessionconfiguration-httpadditionalheaders-not-set

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!