授权标头在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];
答案 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