NSHTTPCookie(Objective-C)官方文檔翻譯

繼承自:NSObject
遵守協(xié)議:NSObject
導(dǎo)入聲明:@import Foundation;
適用范圍:iOS 2.0 及以后

一宾茂、概述(Overview)

一個(gè) NSHTTPCookie 對(duì)象代表一個(gè) HTTP cookie。NSHTTPCookie 對(duì)象是不可變的,它初始化自一個(gè)包含 cookie 屬性的字典链烈。

NSHTTPCookie 類支持兩種不同的 cookie 版本:

  • 版本 0:這種版本指的是傳統(tǒng)的或者老式的 cookie,也就是網(wǎng)景Netscape)公司定義的最初的那種 cookie 格式挚躯。我們遇到的大多數(shù) cookie 都是這種格式强衡。

  • 版本 1:這種版本指的是那些在 RFC 2965漩勤,HTTP State Management Mechanism 中定義的 cookie。

二究飞、功能(Tasks)

1.創(chuàng)建 NSHTTPCookie 實(shí)例(Create cookie instances)
+ cookiesWithResponseHeaderFields:forURL:

Returns an array of NSHTTPCookie objects corresponding to the provided response header fields for the provided URL.

Declaration

+ (NSArray <NSHTTPCookie *> *)cookiesWithResponseHeaderFields:(NSDictionary <NSString *,  NSString *> *)*headerFields*
                                                       forURL:(NSURL *)theURL

Parameters

參數(shù) 含義
headerFields The header fields used to create the NSHTTPCookie objects.
theURL The URL associated with the created cookies.

Return Value
The array of created cookies.

Discussion
This method ignores irrelevant header fields in headerFields, allowing dictionaries to contain additional data.
If headerFields does not specify a domain for a given cookie, the cookie is created with a default domain value of the URL.
If headerFields does not specify a path for a given cookie, the cookie is created with a default path value of "/".

Availability
Available in iOS 2.0 and later.


+ cookieWithProperties:

Creates and initializes an NSHTTPCookie object using the provided properties.

Declaration

+ (NSHTTPCookie *)cookieWithProperties:(NSDictionary <NSString *, id> *)properties

Parameters

參數(shù) 含義
properties The properties for the new cookie object, expressed as key value pairs.

Return Value
The newly created cookie object. Returns nil if the provided properties are invalid.

Discussion
To successfully create a cookie, you must provide values for (at least) the NSHTTPCookiePath, NSHTTPCookieName, and NSHTTPCookieValue keys, and either the NSHTTPCookieOriginURL key or the NSHTTPCookieDomain key.
See Constants for more information on the available cookie attribute constants and the constraints imposed on the values in the dictionary.

Availability
Available in iOS 2.0 and later.

See Also
– initWithProperties:


- initWithProperties:

Returns an initialized NSHTTPCookie object using the provided properties.

Declaration

- (instancetype)initWithProperties:(NSDictionary <NSString *, id> *)properties

Parameters

參數(shù) 含義
properties The properties for the new cookie object, expressed as key value pairs.

Return Value
The initialized cookie object. Returns nil if the provided properties are invalid.

Discussion
To successfully create a cookie, you must provide values for (at least) the NSHTTPCookiePath, NSHTTPCookieName, and NSHTTPCookieValue
keys, and either the NSHTTPCookieOriginURL key or the NSHTTPCookieDomain key.
See Constants for more information on the available cookie attribute constants and the constraints imposed on the values in the dictionary.

Availability
Available in iOS 2.0 and later.

See Also
+ cookieWithProperties:

2.將 cookie 轉(zhuǎn)成請(qǐng)求頭(Convert cookies to request headers)

+ requestHeaderFieldsWithCookies:

Returns a dictionary of header fields corresponding to a provided array of cookies.

Declaration

+ (NSDictionary)<NSString*, NSString *> *)requestHeaderFieldsWithCookies:(NSArray <NSHTTPCookie *> *)cookies

Parameters

參數(shù) 含義
cookies The cookies from which the header fields are created.

Return Value
The dictionary of header fields created from the provided cookies.

Discussion
To send these headers as part of a URL request to a remote server, create an NSMutableURLRequest object, then call the setAllHTTPHeaderFields: or setValue:forHTTPHeaderField: method to set the provided headers for the request. Finally, initialize and start an NSURLSessionTask, NSURLConnection, or NSURLDownload object based on that request object.

Availability
Available in iOS 2.0 and later.

3.獲取 cookie 的屬性(Getting cookie properties)

comment Property

The receiver's comment string. (read-only)

Declaration

@property(readonly, copy) NSString *comment

Discussion
The receiver’s comment string or nil if the cookie has no comment. This string is suitable for presentation to the user, explaining the contents and purpose of this cookie.

Availability
Available in iOS 2.0 and later.


commentURL Property

The receiver’s comment URL. (read-only)

Declaration

@property(readonly, copy) NSURL *commentURL

Discussion
The receiver’s comment URL or nil if the cookie has none. This value specifies a URL which is suitable for presentation to the user as a link for further information about this cookie.

Availability
Available in iOS 2.0 and later.


domain Property

The domain of the receiver’s cookie. (read-only)

Declaration

@property(readonly, copy) NSString *domain

Discussion
If the domain does not start with a dot, then the cookie is only sent to the exact host specified by the domain. If the domain does start with a dot, then the cookie is sent to other hosts in that domain as well, subject to certain restrictions. See RFC 2965 for more detail.

Availability
Available in iOS 2.0 and later.


expiresDate Property

The receiver’s expiration date. (read-only)

Declaration

@property(readonly, copy) NSDate *expiresDate

Discussion
The receiver’s expiration date, or nil if there is no specific expiration date such as in the case of “session-only” cookies. The expiration date is the date when the cookie should be deleted.

Availability
Available in iOS 2.0 and later.


HTTPOnly Property

A boolean value that indicates whether the receiver should only be sent to HTTP servers per RFC 2965. (read-only)

Declaration

@property(readonly, getter=isHTTPOnly) BOOL HTTPOnly

Discussion
Returns YES if this cookie should only be sent via HTTP headers, NO otherwise.
Cookies may be marked as HTTP only by a server (or by a javascript). Cookies marked as such must only be sent via HTTP Headers in HTTP requests for URL's that match both the path and domain of the respective cookies.

IMPORTANT
Cookies specified as HTTP only should not be delivered to any javascript applications to prevent cross-site scripting vulnerabilities.

Availability
Available in iOS 2.2 and later.


secure Property

A boolean value that indicates whether this cookie should only be sent over secure channels. (read-only)

Declaration

@property(readonly, getter=isSecure) BOOL secure

Discussion
YES if this cookie should only be sent over secure channels, otherwise NO.

Availability
Available in iOS 2.0 and later.


sessionOnly Property

A boolean value that indicates whether the receiver should be discarded at the end of the session (regardless of expiration date). (read-only)

Declaration

@property(readonly, getter=isSessionOnly) BOOL sessionOnly

Discussion
YES if the receiver should be discarded at the end of the session (regardless of expiration date), otherwise NO.

Availability
Available in iOS 2.0 and later.


name Property

The receiver’s name. (read-only)

Declaration

@property(readonly, copy) NSString *name

Availability
Available in iOS 2.0 and later.


path Property

The receiver’s path. (read-only)

Declaration

@property(readonly, copy) NSString *path

Discussion
The cookie will be sent with requests for this path in the cookie's domain, and all paths that have this prefix. A path of "/" means the cookie will be sent for all URLs in the domain.

Availability
Available in iOS 2.0 and later.


portList Property

The receiver's port list. (read-only)

Declaration

@property(readonly, copy) NSArray <NSNumber *> *portList

Discussion
The list of ports for the cookie, returned as an array of NSNumber objects containing integers. If the cookie has no port list this method returns nil and the cookie will be sent to any port. Otherwise, the cookie is only sent to ports specified in the port list.

Availability
Available in iOS 2.0 and later.


properties Property

The receiver’s cookie properties. (read-only)

Declaration

@property(readonly, copy) NSDictionary <NSString *,id> *properties

Discussion
This dictionary can be used with initWithProperties: or cookieWithProperties: to create an equivalent NSHTTPCookie object.
See initWithProperties: for more information on the constraints imposed on the properties dictionary.

Availability
Available in iOS 2.0 and later.


value Property

The receiver’s value. (read-only)

Declaration

@property(readonly, copy) NSString *value

Availability
Available in iOS 2.0 and later.


version Property

The receiver’s version. (read-only)

Declaration

@property(readonly) NSUInteger version

Discussion
The receiver's version. Version 0 maps to “old-style” Netscape cookies. Version 1 maps to RFC 2965 cookies.

Availability
Available in iOS 2.0 and later.

三耗拓、常量(Constants)

HTTP Cookie 屬性的 key(HTTP Cookie Attribute Keys

These constants define the supported keys in a cookie attributes dictionary.

Declaration

NSString *NSHTTPCookieComment;
NSString *NSHTTPCookieCommentURL;
NSString *NSHTTPCookieDiscard;
NSString *NSHTTPCookieDomain;
NSString *NSHTTPCookieExpires;
NSString *NSHTTPCookieMaximumAge;
NSString *NSHTTPCookieName;
NSString *NSHTTPCookieOriginURL;
NSString *NSHTTPCookiePath;
NSString *NSHTTPCookiePort;
NSString *NSHTTPCookieSecure;
NSString *NSHTTPCookieValue;
NSString *NSHTTPCookieVersion;    

Constants

  • NSHTTPCookieComment

    An NSString object containing the comment for the cookie.
    Only valid for Version 1 cookies and later.** This cookie attribute is optional.**

    Available in iOS 2.0 and later.

  • NSHTTPCookieCommentURL

An NSURL object or NSString object containing the comment URL for the cookie.
Only valid for Version 1 cookies or later.** This cookie attribute is optional.**

Available in iOS 2.0 and later.

  • NSHTTPCookieDiscard

An NSString object stating whether the cookie should be discarded at the end of the session.
String value must be either "TRUE" or "FALSE". This cookie attribute is optional. The default is "FALSE", unless this cookie is version 1 or greater and a value for NSHTTPCookieMaximumAge is not specified, in which case it is assumed to be "TRUE".

Available in iOS 2.0 and later.

  • NSHTTPCookieDomain

An NSString object containing the domain for the cookie.
If this cookie attribute is missing, the domain is inferred from the value for NSHTTPCookieOriginURL. If you do not specify a value for NSHTTPCookieOriginURL, you must specify a value for NSHTTPCookieDomain.

Available in iOS 2.0 and later.

  • NSHTTPCookieExpires

An NSDate object or NSString object specifying the expiration date for the cookie.
This cookie attribute is only used for Version 0 cookies.** This cookie attribute is optional.**

Available in iOS 2.0 and later.

  • NSHTTPCookieMaximumAge

An NSString object containing an integer value stating how long in seconds the cookie should be kept, at most.
Only valid for Version 1 cookies and later. Default is "0". This cookie attribute is optional.

Available in iOS 2.0 and later.

  • NSHTTPCookieName

An NSString object containing the name of the cookie. This cookie attribute is required.

Available in iOS 2.0 and later.

  • NSHTTPCookieOriginURL

An NSURL or NSString object containing the URL that set this cookie.
If you do not provide a value for NSHTTPCookieOriginURL, you must provide a value for NSHTTPCookieDomain.

Available in iOS 2.0 and later.

  • NSHTTPCookiePath

An NSString object containing the path for the cookie.
This cookie attribute is required.

Available in iOS 2.0 and later.

  • NSHTTPCookiePort

An NSString object containing comma-separated integer values specifying the ports for the cookie.
Only valid for Version 1 cookies or later. The default value is an empty string (""). This cookie attribute is optional.

Available in iOS 2.0 and later.

  • NSHTTPCookieSecure

An NSString object indicating that the cookie should be transmitted only over secure channels.
Providing any value for this key indicates that the cookie should remain secure.

Available in iOS 2.0 and later.

  • NSHTTPCookieValue

An NSString object containing the value of the cookie.
This cookie attribute is required.

Available in iOS 2.0 and later.

  • NSHTTPCookieVersion

An NSString object that specifies the version of the cookie.
Must be either "0" or "1". The default is "0". This cookie attribute is optional.

Available in iOS 2.0 and later.

參考(Reference):
https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSHTTPCookie_Class/index.html#//apple_ref/occ/cl/NSHTTPCookie


問題(Question):
1.Cookie 是什么?有哪些屬性负甸?百度百科-cookieWikipedia-HTTP cookie
2.Cookie 有幾種格式?
3.RFC 是什么迫淹?百度百科-RFC梗脾,Wikipedia-RFC
4.Cookie 和 Session 的區(qū)別和聯(lián)系瑞妇?

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子偶翅,更是在濱河造成了極大的恐慌,老刑警劉巖形导,帶你破解...
    沈念sama閱讀 222,681評(píng)論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件淋叶,死亡現(xiàn)場(chǎng)離奇詭異芬膝,居然都是意外死亡桐早,警方通過查閱死者的電腦和手機(jī)友存,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,205評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門搀军,熙熙樓的掌柜王于貴愁眉苦臉地迎上來焚刺,“玉大人兄淫,你說我怎么就攤上這事。” “怎么了挖滤?”我有些...
    開封第一講書人閱讀 169,421評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)惧盹。 經(jīng)常有香客問我,道長(zhǎng),這世上最難降的妖魔是什么诊沪? 我笑而不...
    開封第一講書人閱讀 60,114評(píng)論 1 300
  • 正文 為了忘掉前任挤悉,我火速辦了婚禮橄仆,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘您宪。我一直安慰自己,他們只是感情好捏卓,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,116評(píng)論 6 398
  • 文/花漫 我一把揭開白布浴捆。 她就那樣靜靜地躺著,像睡著了一般梯捕。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上锣笨,一...
    開封第一講書人閱讀 52,713評(píng)論 1 312
  • 那天隆豹,我揣著相機(jī)與錄音判哥,去河邊找鬼挺身。 笑死热芹,一個(gè)胖子當(dāng)著我的面吹牛府寒,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播邪狞,決...
    沈念sama閱讀 41,170評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了吁津?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 40,116評(píng)論 0 277
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎河闰,沒想到半個(gè)月后姜性,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體印机,經(jīng)...
    沈念sama閱讀 46,651評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡奶是,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,714評(píng)論 3 342
  • 正文 我和宋清朗相戀三年初嘹,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了坷随。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,865評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖闯冷,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情蒂窒,我是刑警寧澤,帶...
    沈念sama閱讀 36,527評(píng)論 5 351
  • 正文 年R本政府宣布,位于F島的核電站呛踊,受9級(jí)特大地震影響砾淌,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜谭网,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,211評(píng)論 3 336
  • 文/蒙蒙 一汪厨、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧愉择,春花似錦、人聲如沸旧乞。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,699評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至橘茉,卻和暖如春拜马,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背馁龟。 一陣腳步聲響...
    開封第一講書人閱讀 33,814評(píng)論 1 274
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留爹谭,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 49,299評(píng)論 3 379
  • 正文 我出身青樓贡未,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親棍苹。 傳聞我的和親對(duì)象是個(gè)殘疾皇子嚼沿,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,870評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容