4

我使用以下代码创建 cookie,但失败了。(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?

4

4 回答 4

8

NSHTTPCookieOriginURL使用请求的 URL时似乎存在问题。

尝试使用此代码,它对我有用:

// 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在这里不起作用。

希望这可以帮助,

于 2013-04-12T08:02:01.150 回答
2

要成功创建 cookie,您必须(至少)为 NSHTTPCookiePath、NSHTTPCookieName 和 NSHTTPCookieValue 键以及 NSHTTPCookieOriginURL 键或 NSHTTPCookieDomain 键提供值。

于 2015-02-12T03:50:50.300 回答
2

我遇到了同样的问题。当我查看文档并发现属性只需要'name'、'value'和'originURL'或'domain'时,我尝试了,但失败了。在我添加了“路径”之后它就起作用了。因为我没有准确理解,如果你只提供'originURL'以外的'domain','path'也是必需的。

<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];
于 2018-02-08T15:34:11.700 回答
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);
}];
于 2014-01-20T08:55:38.423 回答