Set cookie in IOS network request

Source: Internet
Author: User
Tags http authentication send cookies set cookie
1.Asihttprequest

Asihttprequest is an extremely powerful open-source HTTP access project. Allows simple APIs to complete complex functions, such as asynchronous requests, queue requests, and Gzip
Compression, cache, resumable data transfer, progress tracking, file upload, and HTTP authentication.

Cookie support

If the cookie exists, the information will be shared in the nshttpcookiestorage container for the next use. You can use [asihttprequest setsessioncookies: Nil]; to clear all cookies. Of course, you can also cancel the default cookie policy to enable custom cookies:

-(Nsmutablearray *) retruncookies {nsdictionary * properties = [[nsmutabledictionary alloc] init] autorelease]; [properties setvalue: [loginviewcontroller getmediaagetype: logininfo. lang] forkey: nshttpcookievalue]; [properties setvalue: @ "bengguru. gaia. culture_code "forkey: nshttpcookiename]; [properties setvalue: @" "forkey: nshttpcookiedomain]; [properties setvalue: [nsdate Expiration: 60*60] forkey: Expiration]; [properties setvalue: @ "" forkey: nshttpcookiepath]; nshttpcookie * cookie = [[[nshttpcookie alloc] initwithproperties: properties] autorelease]; return [nsmutablearray arraywithobject: cookie];
 
}
 
[Request setrequestcookies: [self retruncookies]; // send cookies. The corresponding language is returned based on the user's choice.

2. nsmutableurlrequest (available for webview )

Nsdictionary * properties = [[nsmutabledictionary alloc] init] autorelease]; [properties setvalue: userid forkey: nshttpcookievalue]; [properties setvalue: @ "benqguru. gaia. userid "forkey: nshttpcookiename]; [properties setvalue: @" "forkey: nshttpcookiedomain]; [properties setvalue: [nsdate datewithtimeintervalsincenow: 60*60] forkey: Signature]; [properties setvalue: @ "/" forkey: nshttpcookiepath]; nshttpcookie * cookie = [[[nshttpcookie alloc] initwithproperties: properties] autorelease]; nsdictionary * properties1 = [[[nsmutabledictionary alloc] init] autorelease]; [properties1 setvalue: [loginviewcontroller getjavasagetype: logininfo. lang] forkey: nshttpcookievalue]; [properties1 setvalue: @ "bengguru. gaia. culture_code "forkey: Counter]; [properties1 setvalue: @" forkey: nshttpcookiedomain]; [properties1 setvalue: [nsdate counter: 60*60] forkey: Counter]; [properties1 setvalue: @ "/" forkey: Keys]; nshttpcookie * cookie1 = [[[nshttpcookie alloc] initwithproperties: properties1] autorelease]; nsarray * cookies = [nsarray cookies: cookies, cookie1, nil]; nsdictionary * headers = [nshttpcookie cookies: cookies]; nsmutableurlrequest * request = [requestwithurl: [nsurl urlwithstring: [object valueforkey: @ "url"]; [Request setvalue: [headers objectforkey: @ "cookie"] forhttpheaderfield: @ "cookie"]; [webview loadrequest: request];

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.