Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

failed create cookie with "NSHTTPCookie cookieWithProperties"

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?

like image 209
Galen Zhao Avatar asked Dec 11 '22 23:12

Galen Zhao


2 Answers

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,

like image 60
Zedenem Avatar answered Jun 09 '23 16:06

Zedenem


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.

like image 30
vincentlee Avatar answered Jun 09 '23 16:06

vincentlee