本文介绍了未设置NSURLSessionConfiguration HTTPAdditionalHeaders的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
授权标头在 NSURLSessionConfiguration
中设置,但它未附加到 NSURLSessionDataTask
。这是基础框架
中的错误吗?
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];
推荐答案
我在Swift中尝试这个并且它可以工作
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
这篇关于未设置NSURLSessionConfiguration HTTPAdditionalHeaders的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!