zoukankan      html  css  js  c++  java
  • ASIHTTPRequest-Cookie的使用[转]

    ASIHTTPRequest允许你使用全局存储来和所有使用CFNetwork或者NSURLRequest接口的程序共享cookie。

    如果设置useCookiePersistence为YES(默认值),cookie会被存储在共享的 NSHTTPCookieStorage 容器中,并且会自动被其他request重用。值得一提的是,ASIHTTPRequest会向服务器发送其他程序创建的cookie(如果这些cookie对特定request有效的话)。

    你可以清空session期间创建的所有cookie:

    1
    [ASIHTTPRequest setSessionCookies:nil];

    这里的‘session cookies’指的是一个session中创建的所有cookie,而非没有过期时间的cookie(即通常所指的会话cookie,这种cookie会在程序结束时被清除)。

    另外,有个方便的函数 clearSession可以清除session期间产生的所有的cookie和缓存的授权数据。 

    自己处理cookie

    如果你愿意,你大可以关闭useCookiePersistence,自己来管理某个request的一系列cookie:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    //创建一个cookie
    NSDictionary *properties = [[[NSMutableDictionary alloc] init] autorelease];
    [properties setValue:[@"Test Value" encodedCookieValue] forKey:NSHTTPCookieValue];
    [properties setValue:@"ASIHTTPRequestTestCookie" forKey:NSHTTPCookieName];
    [properties setValue:@".dreamingwish.com" forKey:NSHTTPCookieDomain];
    [properties setValue:[NSDate dateWithTimeIntervalSinceNow:60*60] forKey:NSHTTPCookieExpires];
    [properties setValue:@"/asi-http-request/tests" forKey:NSHTTPCookiePath];
    NSHTTPCookie *cookie = [[[NSHTTPCookie alloc] initWithProperties:properties] autorelease];
     
    //这个url会返回名为'ASIHTTPRequestTestCookie'的cookie的值
    url = [NSURL URLWithString:@"http://www.dreamingwish.com/"];
    request = [ASIHTTPRequest requestWithURL:url];
    [request setUseCookiePersistence:NO];
    [request setRequestCookies:[NSMutableArray arrayWithObject:cookie]];
    [request startSynchronous];
     
    //将会打印: I have 'Test Value' as the value of 'ASIHTTPRequestTestCookie'
    NSLog(@"%@",[request responseString]);
  • 相关阅读:
    C#2.0技术探讨(1):匿名方法
    C#测试类的嵌套
    介绍几款博客发布工具,绝对好用
    HttpModule,对ASP.NET的事件处理进行过滤,干预
    C#中,控制台模式可以使用定时器吗?
    const常量和static静态只读变量有何区别
    C#设计模式(2):工厂模式
    ASP.NET 的数据绑定语法
    DataAdapter数据集DataSet和数据库的同步(2):使用DataAdapter来更新数据集
    TCP编程(5):服务器端 TcpListener
  • 原文地址:https://www.cnblogs.com/sunshine-anycall/p/4135212.html
Copyright © 2011-2022 走看看