NSURLSessionDownloadTask未在请求中发送cookie

时间:2015-10-29 10:55:51

标签: macos cookies nsurlsession

我需要在请求中发送cookie。但似乎cookie不发送。我在这里做错了什么,

// set the cookie for getdoc request url
    if (request.cookie)
    {
        NSDictionary *cookieProperties = @{NSHTTPCookieName     : @"GetDoc",
                                           NSHTTPCookieValue    : request.cookie,
                                           NSHTTPCookieDomain   : [NSURL URLWithString:request.url].host,
//                                         NSHTTPCookieOriginURL: request.url,
                                           NSHTTPCookiePath     : @"/",
//                                         NSHTTPCookieVersion  : @"1",
                                           NSHTTPCookieExpires  : [[NSDate date] dateByAddingTimeInterval:60*60*24*30]
                                           };
        NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];
        [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
    }

    NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
//  NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig];
    NSURLSession *session = [NSURLSession sharedSession];

    __block NSData *documentData = nil;
    __block NSString *mimeType = nil;

    DLog(@"COOKIES: %@", [NSHTTPCookieStorage sharedHTTPCookieStorage].cookies);
    // retrieve the doc from url
    // block the call to wait for response for kMaxBlockingTimeHttpSeconds
    dispatch_semaphore_t sema = dispatch_semaphore_create(0);
    NSURLSessionDownloadTask *getDocTask = [session downloadTaskWithURL:[NSURL URLWithString:request.url]
                                                      completionHandler:^(NSURL *location,
                                                                          NSURLResponse *response,
                                                                        NSError *error)
                                            {
                                                if (error) {
                                                    NSLog(@"Error retrieving document from %@, Error: %@",
                                                          request.url, error);
                                                    res.errorCode = SCPErrorDocumentGetError;
                                                    res.errorMessage = [error localizedDescription];
                                                } else {
                                                    mimeType = response.MIMEType;
                                                    documentData = [NSData dataWithContentsOfURL:location];
                                                }
                                                dispatch_semaphore_signal(sema);
                                            }];
    [getDocTask resume];
    dispatch_time_t timeout = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(kMaxBlockingTimeHttpSeconds * NSEC_PER_SEC));
    dispatch_semaphore_wait(sema, timeout);

    if (!documentData) {
        DLog(@"Response: %@", [res jsonDict]);
        self.data = [NSJSONSerialization dataWithJSONObject:[res jsonDict] options:NSJSONWritingPrettyPrinted error:nil];
        return;
    }

    NSString *str =  [[NSString alloc] initWithData:documentData encoding:NSISOLatin1StringEncoding];
    DLog(@"%@", str);

cookie的NSLog打印输出是, “”,

但是这不会在请求标头中发送。

1 个答案:

答案 0 :(得分:0)

Adding the cookie header directly in the sessionConfig worked for me. But why did not the shareCookie work. This is supposed to work according the the documentation.

NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
    sessionConfig.HTTPAdditionalHeaders = @{@"Cookie" : request.cookie};