gpt4 book ai didi

objective-c - 为什么设置了一个NSHTTPCookie之后,没有附加到NSURLRequest上?

转载 作者:搜寻专家 更新时间:2023-10-30 19:46:02 24 4
gpt4 key购买 nike

我正在设置(或试图设置)NSHTTPCookie,如下所示:

+ (void)setCookie {

NSString* cookieName = @"MyCookieName";
NSString* cookieValue = @"MyCookieValue";
NSString* cookieOriginURL = @"www.mycompany.com";
NSString* cookiePath = @"/";

NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];
[cookieProperties setObject:cookieName forKey:NSHTTPCookieName];
[cookieProperties setObject:cookieValue forKey:NSHTTPCookieValue];
[cookieProperties setObject:cookieOriginURL forKey:NSHTTPCookieOriginURL];
[cookieProperties setObject:cookiePath forKey:NSHTTPCookiePath];

[cookieProperties setObject:[[NSDate date] dateByAddingTimeInterval:3600] forKey:NSHTTPCookieExpires];

NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];

}

调用此方法后,我创建一个 NSURLRequest:

NSString *urlAddress = @"http//:www.mycompany.com/mobile/home";

//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];

//URL Request Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

NSLog(@"Here's the request: %@", [requestObj description]);

//Load the request in the UIWebView.
[self.webView loadRequest:requestObj];

但是输出是:

Here's the request: <NSURLRequest: 0xa33a4d0> { URL: http:www.mycompany.com/mobile/home

我希望看到附加到请求的 cookie 信息,但事实并非如此。

我对 cookie 了解不多,所以我不知道我的代码是否遗漏了什么,或者我是否只是想念解释输出的含义。

感谢您的帮助。

最佳答案

我怀疑 NSURLRequest 的描述会提供 cookie 信息。

域是匹配的(cookie 和 URL),因此 cookie 必须附加到请求中。在触发请求之前执行以下代码,以查看随您的请求一起发送了哪些 cookie。

NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for(NSHTTPCookie *cookie in [cookieJar cookiesForURL:url]) {
NSLog(@"Cookies attached: %@", cookie.description);
}

关于objective-c - 为什么设置了一个NSHTTPCookie之后,没有附加到NSURLRequest上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20251430/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com