美文网首页
iOS cookie

iOS cookie

作者: 海森V | 来源:发表于2019-01-15 15:52 被阅读4次

    1 NSMutableURLRequest请求时,会找到和该域名相同的cookie值,放到一起,然后请求。

    2 [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie]; 不会在本地磁盘写入,只做内存缓存。

    3 添加cookie

     NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];
        [cookieProperties setObject:@"authKey" forKey:NSHTTPCookieName];
        [cookieProperties setObject:@".tabobao.com" forKey:NSHTTPCookieDomain];
        [cookieProperties setObject:@"sfda" forKey:NSHTTPCookieValue];
        [cookieProperties setObject:@"/" forKey:NSHTTPCookiePath];
        [cookieProperties setObject:@"0" forKey:NSHTTPCookieVersion];
    
        NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];
        [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
        
        //添加cookie
        NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
        for (NSHTTPCookie *tempCookie in cookies) {
            NSLog(@"tep %@",tempCookie);
        }
    

    4 遍历cookie

      NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
        for (NSHTTPCookie *tempCookie in cookies) {
            NSLog(@"tep %@",tempCookie);
        }
    

    5 删除cookie

      for (NSHTTPCookie *curCookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
            [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:curCookie];
        }
    

    6测试代码

    - (void)viewDidLoad {
        [super viewDidLoad];
        [self writeCookie];
        [self testquest];
    }
    - (void)writeCookie{
        NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];
        [cookieProperties setObject:@"authKey" forKey:NSHTTPCookieName];
        [cookieProperties setObject:@".taobao.com" forKey:NSHTTPCookieDomain];
        [cookieProperties setObject:@"sfda" forKey:NSHTTPCookieValue];
        [cookieProperties setObject:@"/" forKey:NSHTTPCookiePath];
        [cookieProperties setObject:@"0" forKey:NSHTTPCookieVersion];
    
        //添加cookie
        NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];
        [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
        
        NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
        for (NSHTTPCookie *tempCookie in cookies) {
            NSLog(@"tep %@",tempCookie);
        }
    
        
    }
    - (long long)testquest{
        NSString *nsUrl = [NSString stringWithFormat:@"https://taobao.com"];
        NSMutableURLRequest *nsmRequest = [[NSMutableURLRequest alloc] init] ;
        [nsmRequest setURL:[NSURL URLWithString:nsUrl]];
        [nsmRequest setHTTPMethod:@"HEAD"];
        [nsmRequest setTimeoutInterval:30];
        [nsmRequest addValue:[NSString stringWithFormat:@"text/html"] forHTTPHeaderField: @"Content-Type"];
        [nsmRequest addValue:[NSString stringWithFormat:@"no-cache"] forHTTPHeaderField: @"Cache-Control"];
        
        //get response
        NSHTTPURLResponse* nsResponse = nil;
        NSError *nsError = nil;
        NSData *nsResponseData = nil;
        
        @try
        {
            nsResponseData = [NSURLConnection sendSynchronousRequest:nsmRequest returningResponse:&nsResponse error:&nsError];
        }
        @catch (NSException * e)
        {
            return 0;
        }
        
        
        if( nsResponseData == nil )
        {
            if( -1001 == [nsError code] )
            {
                return 0;
            }
            
            
            return 0;
        }
    }
    

    赋值代码后,使用Charles抓包就好。

    相关文章

      网友评论

          本文标题:iOS cookie

          本文链接:https://www.haomeiwen.com/subject/sahydqtx.html