4
votes

I use following code to create a cookie, but faild.(iOS SDK 5)

// add cookie
NSDictionary *properties = [NSDictionary dictionaryWithObjectsAndKeys:
                            req.URL, NSHTTPCookieOriginURL,
                            @"MLSTORAGE", NSHTTPCookieName,
                            @"1234567890", NSHTTPCookieValue,
                            nil];
NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:properties];
NSLog(@"\nurl: %@\ncookie: %@", req.URL, cookie);
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
//

the log is:

2012-07-26 18:30:49.914 Motilink[15289:707] -[FMWebDAVRequest sendRequest:][Line 154] 
url: http://210.116.114.195:8080/MLServer/storage/
cookie: (null)

any one know how to create a cookie?

4

4 Answers

8
votes

It seems that there is a problem when using NSHTTPCookieOriginURL with your request's URL.

Try using this code, it worked for me :

// add cookie
    NSDictionary *properties = [NSDictionary dictionaryWithObjectsAndKeys:
                                req.URL.host, NSHTTPCookieDomain,
                                req.URL.path, NSHTTPCookiePath,
                                @"MLSTORAGE", NSHTTPCookieName,
                                @"1234567890", NSHTTPCookieValue,
                                nil];
    NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:properties];
    NSLog(@"\nurl: %@\ncookie: %@", req.URL, cookie);
    [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
    //

However, I don't know why NSHTTPCookieOriginURL doesn't work here.

Hope this helps,

2
votes

To successfully create a cookie, you must provide values for (at least) the NSHTTPCookiePath, NSHTTPCookieName, and NSHTTPCookieValue keys, and either the NSHTTPCookieOriginURL key or the NSHTTPCookieDomain key.

2
votes

I have met the same question. When I looked up the documentation and found that only 'name', 'value' and 'originURL' or 'domain' are required of the properties, I tried, but failed. After I added 'path' and then it worked. Because I haven't accurately understood that if you just provide 'domain' other than 'originURL', 'path' is also required.

<tr>
    <th>Property key constant</th>
    <th>Type of value</th>
    <th>Required</th>
    <th>Description</th>
</tr>
<tr>
    <td>NSHTTPCookieName</td>
    <td>NSString</td>
    <td>YES</td>
    <td>Name of the cookie</td>
</tr>
<tr>
    <td>NSHTTPCookieValue</td>
    <td>NSString</td>
    <td>YES</td>
    <td>Value of the cookie</td>
</tr>
<tr>
    <td>NSHTTPCookieDomain</td>
    <td>NSString</td>
    <td>Special, a value for either NSHTTPCookieOriginURL or
    NSHTTPCookieDomain must be specified.</td>
    <td>Domain for the cookie. Inferred from the value for
    NSHTTPCookieOriginURL if not provided.</td>
</tr>
<tr>
    <td>NSHTTPCookieOriginURL</td>
    <td>NSURL or NSString</td>
    <td>Special, a value for either NSHTTPCookieOriginURL or
    NSHTTPCookieDomain must be specified.</td>
    <td>URL that set this cookie. Used as default for other fields
    as noted.</td>
</tr>
<tr>
    <td>NSHTTPCookiePath</td>
    <td>NSString</td>
    <td>NO</td>
    <td>Path for the cookie. Inferred from the value for
    NSHTTPCookieOriginURL if not provided. Default is "/".</td>
</tr>

The final code is below:

NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];
[cookieProperties setObject:@"SESSION" forKey:NSHTTPCookieName];
[cookieProperties setObject:@"value" forKey:NSHTTPCookieValue];
[cookieProperties setObject:@".domain.com" forKey:NSHTTPCookieDomain];
[cookieProperties setObject:@"/" forKey:NSHTTPCookiePath];
NSHTTPCookie *co = [NSHTTPCookie cookieWithProperties:cookieProperties];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:co];
1
votes

This might be useful for setting array of cookies in NSHTTPCookieStorage. I was facing this problem and i solved using below code. Hope this will be helpful for someone who is trying to set array of cookies.

 NSDictionary *cookieProperties = [NSDictionary dictionaryWithObjectsAndKeys:
                                  @".domain.com", NSHTTPCookieDomain,
                                  @"/", NSHTTPCookiePath,
                                  @"SESSION", NSHTTPCookieName,
                                  @"Session value",NSHTTPCookieValue,nil];
NSDictionary *cookieProperties1 = [NSDictionary dictionaryWithObjectsAndKeys:
                                  @".domain.com", NSHTTPCookieDomain,
                                  @"/", NSHTTPCookiePath,
                                  @"some cookie", NSHTTPCookieName,
                                  @"some cookie value",NSHTTPCookieValue,nil];

NSHTTPCookie *cookie1 = [NSHTTPCookie cookieWithProperties:cookieProperties1];
NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];
NSArray* cookieArray = [NSArray arrayWithObjects: cookie,cookie1, nil];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:cookieArray forURL:[NSURL   URLWithString:urlString] mainDocumentURL:nil];

You can cross verify your cookies using below code.

[[[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

    NSLog(@"Printing cookies %@", obj);
}];