授权标头在NSURLSessionConfiguration中设置,但它未附加到NSURLSessionDataTask.这是Foundation框架中的错误吗?
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中尝试这个并且它可以工作
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