NSURLRequest doesn't send cookies
Asked Answered
M

2

13

I'm developing a newsstand application and use NSURLRequest to download issue assets.

NSArray *contents = [issue.tableOfContents objectForKey:kSNTableOfContentsContents];
NSHTTPCookie *cookie;
NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSLog(@"HERE GO MY COOKIES");
for (cookie in [cookieJar cookies]) {
    NSLog(@"%@", cookie);
}            
for (NSDictionary *contentItem in contents) {
    NSString *contentURL_string = [contentItem objectForKey:kSNTableOfContentsRemoteURL];
    NSURL *contentURL = [NSURL URLWithString:contentURL_string];
    NSString *fileName = [contentItem objectForKey:kSNTableOfContentsContentsURL];      
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:contentURL];
    NKAssetDownload *asset = [newsstandIssue addAssetWithRequest:request];
    [request release];
    ....
    [asset downloadWithDelegate:self];
    ....
}

When the first for loop is executed my cookies appear to be in NSHTTPCookieStorage, but when actual requests are sent, there are no cookie information in headers. I use CharlesProxy to look that up. Could anyone please give some advice what might be causing this issue?

Mispronounce answered 30/3, 2012 at 16:27 Comment(1)
You could try doing it manually: #5954882Sperling
A
18

From this thread, the magic incantation appears to be:

NSDictionary * headers = [NSHTTPCookie requestHeaderFieldsWithCookies:
  [cookieJar cookies]];
[request setAllHTTPHeaderFields:headers];

(Warning: untested code.)

This will convert your cookie jar into an array of cookies, then to an NSDictionary of headers, and finally, staple those headers to your request. This is comparable to doing it manually, as Adam Shiemke linked in the question errata, but much cleaner in my opinion.

As per the documentation, you may also want to check HTTPShouldHandleCookies to see if your default cookie policy is being used properly.

Armored answered 6/4, 2012 at 5:11 Comment(3)
Works like a boss, but you should note that the OP must create an NSMutableURLRequest in order to be able to use setAllHTTPHeaderFields method. Simply change NSURLRequest *request = [[NSURLRequest alloc] initWithURL:contentURL]; to NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:contentURL];Netty
also, this seems like a great opportunity for a category, and a convenience method one could use along the lines of [myMutableURLRequest addAllCookies]Netty
Awesome +1 @ArmoredParodist
O
1

On iOS projects I found the ASIHTTPRequest very useful for this kind of problems. It does things like authentication and cookies a lot better that the build-in functions: http://allseeing-i.com/ASIHTTPRequest/

Orthodontia answered 7/4, 2012 at 9:53 Comment(4)
Thanks, I use this as well, but I couldn't find a way to make it work with Newsstand Kit. Do you know if they can work together?Mispronounce
Not sure, I haven't used this combination yet. You might find out more at allseeing-i.com/ASIHTTPRequest/How-to-use#persistent_cookiesOrthodontia
ASIHTTPRequest is no longer supported by Ben Copsey who wrote it (last update May 2011) and he recommends not using it for new projectsSelfregulated
Yeah, I'm transferring all my projects to RestKit now :)Mispronounce

© 2022 - 2024 — McMap. All rights reserved.