Create a cookie for NSURLRequest?
Asked Answered
G

5

24

I'm trying to send an authentication string via cookie in a NSMutableURLRequest. I'm trying to create the NSHTTPCookie through

 +(id)cookieWithProperties:(NSDictionary *)properties

But nowhere have I been able to find how to specify the properties other than the simple key-value pair I have for authentication. When I only use my key-value pair, nil is returned.

Any examples, documentation, or thoughts on this would be greatly appreciated.

Gagne answered 27/3, 2009 at 23:1 Comment(1)
Please post the code you're using to create the cookie; did you take a look at developer.apple.com/DOCUMENTATION/Cocoa/Reference/Foundation/…Offertory
F
21

This is how you set properties in a cookie:

 NSDictionary *properties = [NSDictionary dictionaryWithObjectsAndKeys:
                              url, NSHTTPCookieOriginURL,
                              @"testCookies", NSHTTPCookieName,
                              @"1", NSHTTPCookieValue,
                              nil];
  NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:properties];

In the example above: url, testCookies, and 1 are the values. Likewise, NSHTTPCookieOriginURL, NSHTTPCookieName, NSHTTPCookieValue are the keys for the NSDictionary object, as in key-value pairs.

You set/get properties using NSDictionary and add to NSHTTPCookie.

Fastidious answered 28/3, 2009 at 19:23 Comment(2)
Actually i was not able to get it work, only with NSHTTPCookieDomain, and NSHTTPCookiePath. See jm's answer. Also: lists.apple.com/archives/Webkitsdk-dev/2003/Sep/msg00003.htmlPsf
I was able to use NSHTTPCookieOriginURL as long as I also specified NSHTTPCookiePath (thanks to jm below for the hint). Note that you can use the path method on the url to provide the value (i.e. [url path]).Povertystricken
T
40

I noticed on, on my 2.2.1 iPhone, that the cookie didn't get created if NSHTTPCookiePath is not specified, even though it is shown as "optional" in the docs:

So, I do:

NSDictionary *properties = [NSDictionary dictionaryWithObjectsAndKeys:
                            @"example.com", NSHTTPCookieDomain,
                            @"/", NSHTTPCookiePath,  // IMPORTANT!
                            @"testCookies", NSHTTPCookieName,
                            @"1", NSHTTPCookieValue,
                            nil];
NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:properties];

NSArray* cookies = [NSArray arrayWithObjects: cookie, nil];

NSDictionary * headers = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];

[request setAllHTTPHeaderFields:headers];
Tempestuous answered 2/7, 2009 at 5:30 Comment(0)
F
21

This is how you set properties in a cookie:

 NSDictionary *properties = [NSDictionary dictionaryWithObjectsAndKeys:
                              url, NSHTTPCookieOriginURL,
                              @"testCookies", NSHTTPCookieName,
                              @"1", NSHTTPCookieValue,
                              nil];
  NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:properties];

In the example above: url, testCookies, and 1 are the values. Likewise, NSHTTPCookieOriginURL, NSHTTPCookieName, NSHTTPCookieValue are the keys for the NSDictionary object, as in key-value pairs.

You set/get properties using NSDictionary and add to NSHTTPCookie.

Fastidious answered 28/3, 2009 at 19:23 Comment(2)
Actually i was not able to get it work, only with NSHTTPCookieDomain, and NSHTTPCookiePath. See jm's answer. Also: lists.apple.com/archives/Webkitsdk-dev/2003/Sep/msg00003.htmlPsf
I was able to use NSHTTPCookieOriginURL as long as I also specified NSHTTPCookiePath (thanks to jm below for the hint). Note that you can use the path method on the url to provide the value (i.e. [url path]).Povertystricken
J
7

I could not get that to work.

I got this to work however:

NSMutableURLRequest* ret = [NSMutableURLRequest requestWithURL:myURL];
[ret setValue:@"myCookie=foobar" forHTTPHeaderField:@"Cookie"];
Justinjustina answered 16/7, 2009 at 3:59 Comment(0)
B
7

I've found one mistake in jm's example: NSHTTPCookiePath should be @"/", but not @"\\\\".

NSDictionary *properties = [NSDictionary dictionaryWithObjectsAndKeys:
                            @"example.com", NSHTTPCookieDomain,
                            @"/", NSHTTPCookiePath,  // IMPORTANT!
                            @"testCookies", NSHTTPCookieName,
                            @"1", NSHTTPCookieValue,
                            nil];
NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:properties];

NSArray* cookies = [NSArray arrayWithObjects: cookie, nil];

NSDictionary * headers = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];

[request setAllHTTPHeaderFields:headers];
Blouse answered 15/12, 2011 at 8:55 Comment(1)
I updated my answer. Seems reasonable, although I don't have an iphone to test on anymore.Tempestuous
E
0

key NSHTTPCookiePath should exist in dictionary when using

[NSHTTPCookie cookieWithProperties:dictionary]

method whether using NSHTTPCookieDomain or NSHTTPCookieOriginURL. And value for NSHTTPCookiePath should be @"/" not @"\\".

Eurythmic answered 18/4, 2013 at 9:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.