我正在使用以下代码访问网址:
NSData *responseData = [NSURLConnection sendSynchronousRequest:theRequestHttp returningResponse:&theResponse error:&theError];
我在这里初始化了theRequestHttp:
NSMutableURLRequest *theRequestHttp = [[NSMutableURLRequest alloc] initWithURL:url];
没有我在哪里将cookie添加到urlrequest中...事实上,我有以下设置:
[theRequestHttp addValue:nil forHTTPHeaderField:@"Cookie"];
我也试过
[theRequestHttp setValue:nil forHTTPHeaderField:@"Cookie"];
有没有办法不添加Cookie?
答案 0 :(得分:6)
[request setHTTPShouldHandleCookies:NO];
答案 1 :(得分:1)
我不知道您是否可以禁用自动添加Cookie,但您可以随时使用以下代码删除Cookie
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie *cookie in storage.cookies) {
[storage deleteCookie:cookie];
}
答案 2 :(得分:1)
在这里,这是一个执行此操作的方法,此方法在实现您自己的cookie信息后设置cookie,并且此方法下面的类别为此方法提供支持,这将通过在您创建之后立即狙击EXSISTING cookie来实现上面的cookie方法,然后你将cookie变为你想要的任何数据,你仍然会有一个cookie,但除非你用真实信息替换cookie信息,否则它将没有用。您可以通过immediatley浏览一系列cookie并删除它们。无论哪种方式,你都可以让饼干无法正常工作并摆脱它们:
- (void)manipulateCookie {
NSArray* cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
for (NSHTTPCookie* cookie in cookies) {
NSString *alipaySetCookieString = @"CAKEPHP=nil; path=#; domain=#; expires=Wed, 30-Nov-2001 01:01:01 GMT";
NSHTTPCookie * clok = [alipaySetCookieString cookie];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:clok];
}
}
类别:
的NSString + Cookie.h
@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