Failed to create cookie with "NSHTTPCookie cookieWithProperties"

I use the following code to create a cookie, but with an error (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]; // 

Journal:

 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) 

Does anyone know how to create a cookie?

+4
source share
4 answers

There seems to be a problem when using NSHTTPCookieOriginURL with your request 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 do not know why NSHTTPCookieOriginURL does not work here.

Hope this helps,

+6
source

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

+2
source

This can be useful for setting an array of cookies in NSHTTPCookieStorage. I ran into this problem and I decided to use the code below. Hope this will be useful for anyone trying to set an 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-check your cookies using the code below.

 [[[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { NSLog(@"Printing cookies %@", obj); }]; 
0
source

I met the same question. When I looked through the documentation and found that the properties only require a "name", "value" and "originURL" or "domain", I tried, but could not. After I added the "path" and then it worked. Since I just did not understand that if you just provide a "domain" other than "originURL", a "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]; 
0
source

All Articles