douchuitang0331 2015-02-25 14:34
浏览 34
已采纳

iOS中的HTTP会话cookie

I'm a novice iOS developer. I have an application which requires login and it has functions which make HTTP requests through out the app. As per my understanding, When a login happens server sends the app a unique session cookie which we need to use with further HTTP requests in the same session. My method which makes HTTP request is as below. All the requests in my application use this same method.

+ (NSData*)makeHTTPPostRequestToURL:(NSURL *)url withPostString:(NSString *)postString{

    if (!postString)
    {
        postString = [NSString stringWithFormat:@""];
    }

    NSMutableURLRequest* urlRequest = [NSMutableURLRequest requestWithURL:url];

    NSString *authStr = [NSString stringWithFormat:@"genuser:genuser"];
    NSData *authData = [authStr dataUsingEncoding:NSASCIIStringEncoding];
    NSString *authValue = [NSString stringWithFormat:@"Basic %@", [authData base64EncodedStringWithOptions:0]];
    [urlRequest setHTTPMethod:@"POST"];
    [urlRequest setValue:authValue forHTTPHeaderField:@"Authorization"];
    [urlRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [urlRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [urlRequest setValue:[NSString stringWithFormat:@"%lu", (unsigned long)postString.length] forHTTPHeaderField:@"Content-Length"];
    postString =[self specialencoding:NSUTF8StringEncoding :postString ];

    NSLog(@"Request log is %@", postString);

    [urlRequest setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
    NSLog(@"POST:ESCAPED:%@",postString);
    NSHTTPURLResponse* urlResponse = nil;
    NSError* error = nil;

    NSData* responseData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&urlResponse error:&error];

    return responseData;
}   

As you can see I'm not using the cookie when I'm making the request in the same session. All the requests work fine. My doubt is how is the request being served in the same session if don't send the cookie with it. Does iOS save the last received cookie for each app and append it with the further requests the app makes. Any insights ?

  • 写回答

1条回答 默认 最新

  • doukuanghuan7582 2015-02-25 14:39
    关注

    So what is happening is that cookies sent back are stored in the NSHTTPCookieStorage object. Whenever requests are sent back to a URL matching the cookies domain and path, the cookie is sent along with the request automatically.

    To check what cookies are there for a particular domain:

    let url = NSURL(string: "https://yourcookiedomain.com")!;
    let cookieStorage = NSHTTPCookieStorage.sharedHTTPCookieStorage();
    let cookies = cookieStorage.cookiesForURL(url) as Array<NSHTTPCookie>;
    
    var cookie: NSHTTPCookie? = nil;
    for c in cookies {
        if (c.name == "cookie_name") {
           cookie = c;
           break;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大