Setting cookies in IOS network requests

Source: Internet
Author: User
Tags http authentication send cookies

This article reprinted to http://tec.5lulu.com/detail/108k0n1e626py8s96.html 1ASIHTTPRequest

ASIHTTPRequest is an extremely robust HTTP access to open source project. Let the simple API complete complex functions such as: Asynchronous request, queue request, GZIP compression, cache, breakpoint continuation, progress tracking, upload file, HTTP authentication.

Support for Cookies

If the Cookie exists, it will be shared in the Nshttpcookiestorage container for the next use. You can use [ASIHTTPRequest setsessioncookies:nil]; Clear all Cookies. Of course, you can also cancel the default cookie policy and make the custom cookie:

  1. -(nsmutablearray*)retruncookies{
  2. nsdictionary *Properties = [[[nsmutabledictionary alloc] Init ] autorelease];
  3. [Properties SetValue: [loginviewcontroller getlanguagetype:logininfo. Lang] forkey:nshttpcookievalue];
  4. [Properties SetValue: @"Bengguru. GAIA. Culture_code " forkey:nshttpcookiename];
  5. [Properties SetValue: @"" forkey:nshttpcookiedomain];
  6. [Properties SetValue: [nsdate datewithtimeintervalsincenow:60 ] forkey:nshttpcookieexpires];
  7. [Properties SetValue: @"" forkey:nshttpcookiepath];
  8. Nshttpcookie *Cookie = [[[Nshttpcookie alloc] Initwithproperties:properties] autorelease];
  9. return [nsmutablearray arraywithobject:cookie];
  10. }
  11. [Request Setrequestcookies: [self retruncookies]]; //Send cookies, depending on the user's choice, to return the appropriate language.

2 nsmutableurlrequest (can be used for WebView)
  1. Nsdictionary *Properties = [[[nsmutabledictionary alloc] init] Autorelease];
  2. [Properties SetValue:userId forkey:nshttpcookievalue];
  3. [Properties SetValue: @"Benqguru. GAIA. USERID " forkey:nshttpcookiename];
  4. [Properties SetValue: @"" forkey:nshttpcookiedomain];
  5. [Properties SetValue: [nsdate datewithtimeintervalsincenow:] Forkey:nshttpcookieexpires];
  6. [Properties SetValue: @"/" forkey:nshttpcookiepath];
  7. nshttpcookie *= [[[nshttpcookie Alloc] Initwithproperties: properties] Autorelease];
  8. nsdictionary *properties1 = [[[ [nsmutabledictionary alloc] init] autorelease];
  9. [properties1 setValue: [loginviewcontroller getlanguagetype:logininfo. Lang] forkey:nshttpcookievalue];
  10. [properties1 setValue: @"Bengguru. GAIA. Culture_code " forkey:nshttpcookiename];
  11. [properties1 setValue: @"" forkey:nshttpcookiedomain];
  12. [properties1 setValue: [nsdate datewithtimeintervalsincenow:] Forkey:nshttpcookieexpires];
  13. [properties1 setValue: @"/" forkey:nshttpcookiepath];
  14. nshttpcookie *= [[[nshttpcookie Alloc] Initwithproperties: properties1] Autorelease];
  15. nsarray *cookies=[nsarray arraywithobjects:cookies,cookie1, Nil];
  16. nsdictionary *headers=[nshttpcookie requestheaderfieldswithcookies: Cookies];
  17. nsmutableurlrequest *request=[nsmutableurlrequest requestwithurl: [nsurl URLWithString: [object valueforkey: @"url" ]];
  18. [Request SetValue: [headers Objectforkey: @"Cookie"] Forhttpheaderfield : @"Cookie"];
  19. [webView loadrequest:request];

Setting cookies in IOS network requests

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.