Setting cookies in IOS network requests

Source: Internet
Author: User
Tags http authentication send cookies

1. ASIHTTPRequest

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:

[Plain]View Plaincopy
  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*60] forkey:nshttpcookieexpires];
  7. [Properties setvalue:@ "" Forkey:nshttpcookiepath];
  8. Nshttpcookie *cookie = [[[Nshttpcookie alloc] initwithproperties:properties] autorelease];
  9. return [Nsmutablearray Arraywithobject:cookie];
[Plain]View Plaincopy
    1. }
[Plain]View Plaincopy
    1. [Request Setrequestcookies:[self retruncookies]; Send cookies, depending on the user's choice, to return to the appropriate language.




2. nsmutableurlrequest (can be used for WebView) [Plain]View Plaincopy
  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:60*60] forkey:nshttpcookieexpires];
  6. [Properties setvalue:@ "/" forkey:nshttpcookiepath];
  7. Nshttpcookie *cookie = [[[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:60*60] forkey:nshttpcookieexpires];
  13. [Properties1 setvalue:@ "/" forkey:nshttpcookiepath];
  14. Nshttpcookie *cookie1 = [[[Nshttpcookie alloc] initwithproperties:properties1] autorelease];
  15. Nsarray *cookies=[nsarray Arraywithobjects:cookie,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.