Use of cookies in iOS development ASIHTTPRequest

Source: Internet
Author: User
Tags send cookies

This article describes how to use cookies in iOS development ASIHTTPRequest, including persistent cookies and self-processing cookies.

Persistent cookie

ASIHTTPRequest allows you to use global storage to share cookies with all programs that use the CFNetwork or NSURLRequest interface.

If useCookiePersistence is set to YES, the cookie is stored in the shared NSHTTPCookieStorage container and is automatically reused by other requests. It is worth mentioning that ASIHTTPRequest will send cookies created by other programs to the server if these cookies are valid for specific requests ).

You can clear all the cookies created during the session:

 
 
  1. [ASIHTTPRequest setSessionCookies:nil]; 

Here, 'session cookies 'refer to all the cookies created in a session, rather than those with no expiration time, this cookie will be cleared at the end of the program ).

In addition, there is a convenient function clearSession to clear all cookies generated during the session and cache authorization data.

Process cookies by yourself

If you want to, you can close useCookiePersistence and manage a series of cookies for a request by yourself:

 
 
  1. // Create a cookie
  2. NSDictionary * properties = [[NSMutableDictionary alloc] init] autorelease];
  3. [Properties setValue: [@ "Test Value" encodedCookieValue] forKey: NSHTTPCookieValue];
  4. [Properties setValue: @ "ASIHTTPRequestTestCookie" forKey: NSHTTPCookieName];
  5. [Properties setValue: @ ".dreamingwish.com" forKey: NSHTTPCookieDomain];
  6. [Properties setValue: [NSDate dateWithTimeIntervalSinceNow: 60*60] forKey: NSHTTPCookieExpires];
  7. [Properties setValue: @ "/asi-http-request/tests" forKey: NSHTTPCookiePath];
  8. NSHTTPCookie * cookie = [[NSHTTPCookie alloc] initWithProperties: properties] autorelease];
  9.  
  10. // This url returns the cookie value named 'asihttprequesttestcookie '.
  11. Url = [NSURL URLWithString: @ "http://www.dreamingwish.com/"];
  12. Request = [ASIHTTPRequest requestWithURL: url];
  13. [Request setUseCookiePersistence: NO];
  14. [Request setRequestCookies: [NSMutableArray arrayWithObject: cookie];
  15. [Request startSynchronous];
  16.  
  17. // The output will be: I have 'test value' as the Value of 'asihttprequesttestcookie'
  18. NSLog (@ "% @", [request responseString]);

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.