Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the function of Cookie?

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains the "what is the role of Cookie", the content of the explanation is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "what is the role of Cookie" bar!

one

What is Cookie?

Cookie

Cookie, sometimes in its plural form Cookies, refers to the data (usually encrypted) stored on the user's local terminal by some websites in order to identify the user and perform session tracking.

Episode Session

Session

Because the HTTP protocol is stateless, when the server needs to record the user status, it needs to use some mechanism to identify the specific user, this mechanism is Session.

two

What's the use of Cookie?

The author takes the simplified book as an example to introduce the usefulness of Cookie:

(1) when we use a browser to access jianshu.com, the summary server does not know the information of the browser. By default, the browser displays the content as "simplified".

(2) when we set the browser display content to "traditional Chinese", the browser will display traditional Chinese content.

(3) when we close the browser and reopen the browser, we find that the display content of the brief book will still be traditional. The author thinks that the reason is that the simple book server may have made a unique identification record for the browser and placed it in its own Session. When the browser restarts, it will go to the server to request the content of the short book, and find that the current browser needs to be displayed in traditional Chinese, so the traditional Chinese display content will be distributed.

(4) if Cookie is not used because the HTTP protocol is stateless, then when we set the display content to traditional or simplified, when we open a new page or close the browser and reopen it, the previously set display traditional or simplified will no longer exist.

three

Type of Cookie

Generally speaking, Cookie can be divided into two categories: conversational Cookie and persistent Cookie

A session Cookie is a temporary Cookie that records the user's access settings and preferences for long points. When the user exits the browser, the session Cookie is deleted.

Persistent Cookie live longer, they are stored on the hard disk, the browser exits, and when the computer restarts, they still exist. Persistent Cookie is typically used to maintain the configuration file or login name of a site that a user visits periodically.

The only difference between a session Cookie and a persistent Cookie is their expiration time. When no Expires (expiration time) is specified, the default is session Cookie.

Take the brief book as an example: take a look at the conversational Cookie and persistent Cookie of the brief book:

The figure above shows the cookies of jianshu.com

Persistent Cookie:

Expires expires on Tuesday, April 9, 2018, Tue,09 Apr 2019 13:31:57-0000

The author guesses that this value means that 0000 is subtracted from the current time in order to achieve the purpose of being a persistent Cookie.

Domain is .jianshu.com

Path is /

Secure is YES

Http only is true

Session Cookie:

Local:zh-CN is displayed as simplified

Including the session Cookie, I thought that the session Cookie will disappear after closing the browser, but the following test results are not sure whether it can be regarded as a manifestation of the disappearance of the session Cookie. The session Cookie displayed by starting the browser for the first time is:

Local:zh-CN

Path:/

Default_font:font1

If you have set up traditional Chinese characters:

Local:zh-TW

Path:/

Default_font:font1

Then refresh the jianshu.com, display the content, and only display

Local:zh-CN

Or

Local:zh-TW

I'm not sure if this is a session. Cookie disappeared after closing the browser. Readers can also test it themselves. If any of the students on the server side know, please let me know.

four

The workflow of Cookie

The author still takes the browser to open the brief book to show that the font is "simplified" or "traditional" as an example to illustrate the workflow of Cookie.

(1) when we use the browser to access jianshu.com for the first time, the brief book server does not know the information of the browser, and by default, the browser displays the content as "simplified". The server creates a Session for the browser

(2) when we set the browser display content to "traditional", we will set local to zh-TW by Cookie, send a request to the server, and the response will be to set the browser display content to "traditional", and the server colleagues will update the information in the Session to zh-TW (traditional).

(3) when we close the browser and the session disappears, then we reopen the browser and find that the content of the brief book will still be traditional. The author thinks that this is because the brief book server Session stores the font that the browser should display (for example, the server stores a unique id of the browser, and then when the browser is reopened to make a request to the server. The server sends traditional content according to the previous Session.

five

Properties of Cookie

Domain (domain): the domain of Cookie; the browser only sends Cookie to the hostname of the server in the specified domain, so that the server restricts the Cookie to a specific domain. The jianshu.com domain matches jianshu1.jianshu.com and jianshu1.jianshu2.jianshu.com, but not js.com.

Path (path): this property allows you to assign a Cookie to a specific document on the server. If the Path attribute is a URL path prefix, you can append a Cookie, path / foo, which matches / foobar and foo/bar.html, and the path "/" matches everything in the domain name.

Secure (security): whether to send this Cookie only if HTTP uses a SSL connection

Expires (expiration): the number of seconds expired since 00:00:00 GMT on January 1, 1970

Name (name): the name of the Cookie variable

Value: the value of the Cookie variable

six

NSHTTPCookie in iOS

Common attributes:

Domain of NSHTTPCookieDomain domain:cookie

Path of NSHTTPCookiePath path:Cookie

Port list for NSHTTPCookiePort portList:Cookie

The name of NSHTTPCookieName name:Cookie

The value of NSHTTPCookieValue value:Cookie

NSHTTPCookieVersion version: the version of Cookie

Expiration time of NSHTTPCookieExpires expireDate:Cookie

NSHTTPCookieDiscard sessionOnly: a Boolean value indicating whether cookie should be discarded at the end of the session (regardless of expiration date)

HTTPOnly: specifies that clients should not share Cookie with JavaScript applications to prevent cross-site scripting attacks

NSHTTPCookieSecure secure: specifies that Cookie will only be used for HTTPS connections, not HTTP connections

Properties of properties:Cookie

NSHTTPCookiePropertyKey: defines the constants supported in the cookie attribute dictionary

Description text of NSHTTPCookieComment comment:Cookie

Description of NSHTTPCookieCommentURL commentURL:cookie URL

Access to NSHTTPCookieAcceptPolicy:Cookie, and NSHTTPCookie is managed by NSHTTPCookieStorage.

NSHTTPCookieAcceptPolicyAlways: stores all cookie

NSHTTPCookieAcceptPolicyNever: cookie will not be stored

NSHTTPCookieAcceptPolicyOnlyFromMainDocumentDomain: save only the Cookie whose domain value matches the requested domain

seven

IOS network requests using Cookie

The header,key of the cookie in the client's request header is "cookie"

When the server responds to the client, the header,key of the cookie in the response header is "set-cookie"

The network request uses AFN to carry Cookie to test AFN. The network request carries Cookie. The author uses Cookie when accessing juejin.im. The effect is as follows:

The related code is as follows:

NSString * urlString = @ "https://juejin.im"; AFHTTPSessionManager * sessionManager = [AFHTTPSessionManager manager]; sessionManager.responseSerializer = [AFHTTPResponseSerializer serializer]; [sessionManager.requestSerializer setValue:@" QiShareNameAFN=QiShareValueAFN;QiShareTokenAFN=QiShareTokenValueAFN "forHTTPHeaderField:@" cookie "]; [sessionManager GET:urlString parameters:nil progress:nil success: ^ (NSURLSessionDataTask * _ Nonnull task, id _ Nullable responseObject) {} failure: ^ (NSURLSessionDataTask * _ Nullable task, NSError * _ Nonnull error) {}]

When you need to set multiple cookie values, use cookieKey1=cookieValue1; cookieKey2=cookieValue2; to separate each pair of cookieKey and Value with a semicolon.

Network requests use NSURLSession to carry Cookie

Test the NSURLSession network request with Cookie. The author uses Cookie when accessing jianshu.com. The effect is as follows:

The related code is as follows:

NSURL * url = [NSURL URLWithString:@ "https://www.jianshu.com"]; NSMutableURLRequest * mRequest = [NSMutableURLRequest requestWithURL:url]; mRequest.HTTPMethod = @" GET "; [mRequest setValue:@" QiShareName=QiShareValue;QiShareToken=QiShareTokenValue "forHTTPHeaderField:@" cookie "]; NSURLSession * session = [NSURLSession sharedSession]; NSURLSessionDataTask * dataTask = [session dataTaskWithRequest:mRequest completionHandler: ^ (NSData * _ Nullable data, NSURLResponse * _ Nullable response, NSError * _ Nullable error) {}]; [dataTask resume] Thank you for your reading, the above is the content of "what is the role of Cookie", after the study of this article, I believe you have a deeper understanding of the role of Cookie, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report