我使用以下代码创建一个cookie,但是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];
//
日志是:
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)
任何人都知道如何创建cookie吗?
答案 0 :(得分:6)
将NSHTTPCookieOriginURL
与您的请求的网址一起使用时似乎存在问题。
尝试使用此代码,它对我有用:
// 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];
//
但是,我不知道为什么NSHTTPCookieOriginURL
在这里不起作用。
希望这有帮助,
答案 1 :(得分:2)
要成功创建cookie,您必须为(至少)NSHTTPCookiePath,NSHTTPCookieName和NSHTTPCookieValue键以及NSHTTPCookieOriginURL键或NSHTTPCookieDomain键提供值。
答案 2 :(得分:2)
我遇到了同样的问题。当我查阅文档时发现只有' name',' value'和" originURL'或者'域名'我试过要求属性,但失败了。在我添加了“路径”之后然后它奏效了。因为我还没有准确地理解,如果你只提供域名'除了" originURL','路径'也是必需的。
<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>
最终代码如下:
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];
答案 3 :(得分:1)
这可能对在NSHTTPCookieStorage中设置cookie数组很有用。我正面临这个问题,我使用下面的代码解决了。希望这对试图设置一系列cookie的人有所帮助。
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];
您可以使用以下代码交叉验证您的Cookie。
[[[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"Printing cookies %@", obj);
}];