IOS uses cookies in HTTP requests

Source: Internet
Author: User
<span id="Label3"></p>Original works, allow reprint, please be sure to use hyperlinks in the form of the original source of the article, author information and this Statement. otherwise, the legal liability will be investigated. http://rainbird.blog.51cto.com/211214/805173<p><p>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.</p></p><p><p>So how do you look at cookies? Very simple:</p></p> <table cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="line-numbers">1<br>2<br>3<br>4</td> <td>Nshttpcookiestorage *cookiejar = [nshttpcookiestorage sharedhttpcookiestorage];<br>For (nshttpcookie *cookie in [cookiejar cookies]) {<br>NSLog (@ "%@", cookie);<br>}</td> </tr> </tbody> </table><p><p>This lists all the saved cookies and what if they are currently empty? Ask for a URL at random.</p></p> <table cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="line-numbers">1<br>2<br>3<br>4<br>5<br>6<br>7<br>8<br>9<br>10<br>11<br>12</td> <td>Nsurlrequest *request = [nsurlrequest requestwithurl:[nsurl urlwithstring:@ "http://blog.cnrainbird.com"]<br>Cachepolicy:nsurlrequestreloadignoringlocalandremotecachedata<br>timeoutinterval:3];<br><br>[nsurlconnection Sendsynchronousrequest:request<br>Returningresponse:nil<br>error:nil];<br><br>Nshttpcookiestorage *cookiejar = [nshttpcookiestorage sharedhttpcookiestorage];<br>For (nshttpcookie *cookie in [cookiejar cookies]) {<br>NSLog (@ "%@", cookie);<br>}</td> </tr> </tbody> </table><p><p>is not getting a similar:</p></p> <table cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="line-numbers">1</td> <td><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></td> </tr> </tbody> </table><p><p>The stuff? This is a cookie.</p></p><p><p>How do I empty cookies?</p></p> <table cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="line-numbers">1<br>2<br>3<br>4<br>5</td> <td>Nshttpcookiestorage *cookiejar = [nshttpcookiestorage sharedhttpcookiestorage];<br>Nsarray *_tmparray = [nsarray Arraywitharray:[cookiejar cookies]];<br>For (id obj in _tmparray) {<br>[cookiejar deletecookie:obj];<br>}</td> </tr> </tbody> </table><p><p>So the cookie Disappears.</p></p><p><p>Will look at the cookie, it will also empty the cookie, how to set the specified cookie?</p></p> <table cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="line-numbers">1<br>2<br>3<br>4<br>5<br>6<br>7<br>8<br>9<br>10</td> <td>Nsmutabledictionary *cookieproperties = [nsmutabledictionary dictionary];<br>[cookieproperties setobject:@ "username" forkey:nshttpcookiename];<br>[cookieproperties setobject:@ "rainbird" forkey:nshttpcookievalue];<br>[cookieproperties setobject:@ "cnrainbird.com" forkey:nshttpcookiedomain];<br>[cookieproperties setobject:@ "cnrainbird.com" forkey:nshttpcookieoriginurl];<br>[cookieproperties setobject:@ "/" forkey:nshttpcookiepath];<br>[cookieproperties setobject:@ "0" forkey:nshttpcookieversion];<br><br>Nshttpcookie *cookie = [nshttpcookie cookiewithproperties:cookieproperties];<br>[[nshttpcookiestorage sharedhttpcookiestorage] setcookie:cookie];</td> </tr> </tbody> </table><p><p>That's All you can do. input, do not get the following results:</p></p> <table cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="line-numbers">1</td> <td><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></td> </tr> </tbody> </table><p><p>In fact, The iOS cookie is so Simple. If your Mac supports this, then it's not a lot easier to write a landing script later:)</p></p><p><p>IOS uses cookies in HTTP requests</p></p></span>

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.