Cookies for HTTP requests in iOS view, delete, add

Source: Internet
Author: User

Always think that iOS HTTP request this piece is very simple should not support record, save, or use cookie, but take it for granted, really use time, really studied the discovery is really powerful. After some research briefly I understand: When you visit a website, whether you are willing or unwilling, nsurlrequest will help you take the initiative to record the site you visit the cookie set, and very responsible, when you visit this site next time, Nsurlrequest will take the last saved cookie and continue to request it. The same rule applies to asihttprequest. So when you do some authentication-based network requests, cookies are a perfect solution.

So how do you look at cookies? Very simple:

1
2
3
4
Nshttpcookiestorage *cookiejar = [nshttpcookiestorage sharedhttpcookiestorage];
for (Nshttpcookie *cookie in [Cookiejar cookies]) {
     NSLog (@"%@ ", cookie);
}

This lists all the saved cookies, What if you are currently empty? Ask for a URL at random.

1
2
3
4
5
6
7
8
9
10
11
12
nsurlrequest *request = [nsurlrequest requestwithurl:[nsurl urlwithstring:@ "Http://blog.cnrainbird.com"]
Cachepolicy:nsurlrequestreloadignoriNglocalandremotecachedatA
Timeoutinterval:3];

[Nsurlconnection Sendsynchronousrequest:request
Returningresponse:nil
Error:nil];

Nshttpcookiestorage *cookiejar = [Nshttpcookiestorage sharedhttpcookiestorage];
For (Nshttpcookie *cookie in [Cookiejar cookies]) {
NSLog (@ "%@", cookie);
}

is not getting a similar:

1 <nshttpcookie version:0 Name: "PHPSESSID" value: "evf5rcboo8th1dnl53fs4ukmT2" expiresdate: (null) created:2012-03-13 14:28:13 +0000 (3.53342e+08) sessiononly:true domain: "blog.cnrainbird.com" Path: "/" issecure:false>

The stuff? This is a cookie.

How do I empty cookies?

1
2
3
4
5
&NBSP;   Nshttpcookiestorage *cookiejar = [nshttpcookiestorage sharedhttpcookiestorage];
&NBSP;  nsarray *_tmparray = [Nsarray arraywitharray:[cookiejar cookies]];
&NBSP;  for (id obj in _tmparray) {
&NBSP;     [cookiejar Deletecookie:obj];
&NBSP;  }

So the cookie disappears.

Will look at the cookie, it will also empty the cookie, how to set the specified cookie?

1
2
3
4
5
6
7
8
9
10
Nsmutabledictionary *cookieproperties = [Nsmutabledictionary dictionary];
[Cookieproperties setobject:@ "username" forkey:nshttpcookiename];
[Cookieproperties setobject:@ "Rainbird" forkey:nshttpcookievalue];
[Cookieproperties setobject:@ "cnrainbird.com" forkey:nshttpcookiedomain];
[Cookieproperties setobject:@ "cnrainbird.com" forkey:nshttpcookieoriginurl];
[Cookieproperties setobject:@ "/" forkey:nshttpcookiepath];
[Cookieproperties setobject:@ "0" forkey:nshttpcookieversion];

Nshttpcookie *cookie = [Nshttpcookie cookiewithproperties:cookieproperties];
[[Nshttpcookiestorage Sharedhttpcookiestorage] Setcookie:cookie];

That's all you can do. Input, do not get the following results:

1 <nshttpcookie version:0 Name: "username" value: "Rainbird" expiresdate: (null) created:2012-03-13 14:36:53 +0000 (3.53342e+08) sessiononly:true domain: " cnrainbird.com "Path:"/"issecure:false>

Cookies for HTTP requests in iOS view, delete, add

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.