gpt4 book ai didi

ios - NSHTTPCookieStorage 数组 - 解析值

转载 作者:行者123 更新时间:2023-11-29 12:16:24 27 4
gpt4 key购买 nike

我有一个数组 (NSHTTPCookieStorage) 中的数据...

NSARRAY *httpCookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:aHttpUrl];

我知道存储了 3 个 cookie(我可以在 NSLog 中看到它们)。

我不知道如何根据“名称”获取 1 个特定的 cookie 值。

示例:<NSHTTPCookie version:0 name:\"username\" value:\"namehere\" expiresDate:2035-08-3 23:12:50 +0000 created:2015-08-08 23:12:49 +0000 (4.60768e+08) sessionOnly:FALSE domain:\"somedomain.com\" path:\"/example\" isSecure:FALSE>

...如何获取“值”的内容以便将其作为字符串使用?

最佳答案

像这样

NSArray* cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString:YOUR_NETWORK_URL]];
for (NSHTTPCookie* cookie in cookies)
{
NSLog(@"%@, ", [cookie valueForKey:@"version"]);
NSLog(@"%@, ", [cookie valueForKey:@"name"]);

NSString * this = [cookie valueForKey:@"name"];

NSLog(@"%@, ", [cookie valueForKey:@"value"]);
NSLog(@"%@, ", [cookie valueForKey:@"expiresDate"]);
NSLog(@"%@, ", [cookie valueForKey:@"created"]);
NSLog(@"%@, ", [cookie valueForKey:@"sessionOnly"]);
NSLog(@"%@, ", [cookie valueForKey:@"domain"]);
NSLog(@"%@, ", [cookie valueForKey:@"path"]);
NSLog(@"%@, ", [cookie valueForKey:@"isSecure"]);
}

或者三个 cookies ,

 NSString * this1 =  [cookies[0] valueForKey:@"name"]
NSString * this2 = [cookies[1] valueForKey:@"name"]
NSString * this3 = [cookies[2] valueForKey:@"name"]

等等,等等,这样做将所有使用键值的 cookie

操纵 cookies:

NSString *alipaySetCookieString = @"CAKEPHP=nil; path=#; domain=#; expires=Wed, 30-Nov-2001 01:01:01 GMT";
NSHTTPCookie * clok = [alipaySetCookieString cookie];

使用这个:类别“NSString+Cookie.h”的 header

@interface NSString(Cookie)


- (NSHTTPCookie *)cookie;

@end

类别“NSString+Cookie.m”的实现文件

#import "NSString+Cookie.h"

@implementation NSString(Cookie)

- (NSDictionary *)cookieMap{
NSMutableDictionary *cookieMap = [NSMutableDictionary dictionary];

NSArray *cookieKeyValueStrings = [self componentsSeparatedByString:@";"];
for (NSString *cookieKeyValueString in cookieKeyValueStrings) {
NSRange separatorRange = [cookieKeyValueString rangeOfString:@"="];

if (separatorRange.location != NSNotFound &&
separatorRange.location > 0 &&
separatorRange.location < ([cookieKeyValueString length] - 1)) {
NSRange keyRange = NSMakeRange(0, separatorRange.location);
NSString *key = [cookieKeyValueString substringWithRange:keyRange];
NSString *value = [cookieKeyValueString substringFromIndex:separatorRange.location + separatorRange.length];

key = [key stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
value = [value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
[cookieMap setObject:value forKey:key];

}
}
return cookieMap;
}

- (NSDictionary *)cookieProperties {

NSDictionary *cookieMap = [self cookieMap];

NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];
for (NSString *key in [cookieMap allKeys]) {

NSString *value = [cookieMap objectForKey:key];
NSString *uppercaseKey = [key uppercaseString];

if ([uppercaseKey isEqualToString:@"DOMAIN"]) {
if (![value hasPrefix:@"."] && ![value hasPrefix:@"www"]) {
value = [NSString stringWithFormat:@".%@",value];
}
[cookieProperties setObject:value forKey:NSHTTPCookieDomain];
}else if ([uppercaseKey isEqualToString:@"VERSION"]) {
[cookieProperties setObject:value forKey:NSHTTPCookieVersion];
}else if ([uppercaseKey isEqualToString:@"MAX-AGE"]||[uppercaseKey isEqualToString:@"MAXAGE"]) {
[cookieProperties setObject:value forKey:NSHTTPCookieMaximumAge];
}else if ([uppercaseKey isEqualToString:@"PATH"]) {
[cookieProperties setObject:value forKey:NSHTTPCookiePath];
}else if([uppercaseKey isEqualToString:@"ORIGINURL"]){
[cookieProperties setObject:value forKey:NSHTTPCookieOriginURL];
}else if([uppercaseKey isEqualToString:@"PORT"]){
[cookieProperties setObject:value forKey:NSHTTPCookiePort];
}else if([uppercaseKey isEqualToString:@"SECURE"]||[uppercaseKey isEqualToString:@"ISSECURE"]){
[cookieProperties setObject:value forKey:NSHTTPCookieSecure];
}else if([uppercaseKey isEqualToString:@"COMMENT"]){
[cookieProperties setObject:value forKey:NSHTTPCookieComment];
}else if([uppercaseKey isEqualToString:@"COMMENTURL"]){
[cookieProperties setObject:value forKey:NSHTTPCookieCommentURL];
}else if([uppercaseKey isEqualToString:@"EXPIRES"]){
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
dateFormatter.dateFormat = @"EEE, dd-MMM-yyyy HH:mm:ss zzz";
[cookieProperties setObject:[dateFormatter dateFromString:value] forKey:NSHTTPCookieExpires];
}else if([uppercaseKey isEqualToString:@"DISCART"]){
[cookieProperties setObject:value forKey:NSHTTPCookieDiscard];
}else if([uppercaseKey isEqualToString:@"NAME"]){
[cookieProperties setObject:value forKey:NSHTTPCookieName];
}else if([uppercaseKey isEqualToString:@"VALUE"]){
[cookieProperties setObject:value forKey:NSHTTPCookieValue];
}else{
[cookieProperties setObject:key forKey:NSHTTPCookieName];
[cookieProperties setObject:value forKey:NSHTTPCookieValue];
}
}

if (![cookieProperties objectForKey:NSHTTPCookiePath]) {
[cookieProperties setObject:@"/" forKey:NSHTTPCookiePath];
}
return cookieProperties;
}

- (NSHTTPCookie *)cookie{
NSDictionary *cookieProperties = [self cookieProperties];
NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];
return cookie;
}

@end

关于ios - NSHTTPCookieStorage 数组 - 解析值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31899893/

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