ios android cookie,iOS 中 Cookie 一些可能需要知道的地方

昨天有同事和我说,打一个不同负载的包分别测试下咯,我第一反应有点懵,这个平常真心接触的有点少,于是带着疑惑去了解下,首先可以肯定的 是这个和 Cookie 相关的。

一、Cookie 相关的了解

Cookie 的理解

Cookie 是某些网站为了辨别用户身份而储存在用户本地终端上的数据。

Cookie 就是服务器暂存放在客户端上的一笔资料,好让服务器用于辨别客户端。

Cookie 工作原理

eb4487849af2

Cookie 的工作原理, 图片源自:[cookie原理说明](https://www.qcloud.com/document/product/214/2737)

Cookie 和 负载均衡

eb4487849af2

Cookie 和 负载,图片来源:[【负载均衡小知识】](https://sanwen8.cn/p/3fdKpiJ.html)

当然 负载均衡 有很多方方面的,此处我只作为了解,了解下我们 App 端与其相关联的。

eb4487849af2

自己的理解对不同的负载

#######备注参考:

PS : 会话 cookie 和持久性 cookie 的区别,说白了就是是否包含 到期日期 确定。

二、iOS 中 具体的用 Cookie

我之前用到的地方,主要是写 Cookie 的咯,我们为了测试特意在上线之前再加了一个预发布的环境,就是为了模拟线上环境,其中就是对 Cookie 做标记的处理的。当然有写,肯定还有读 Cookie的。

NSHTTPCookie: cookie 对象

NSHTTPCookieStorage : 管理所有 NSHTTPCookie 的对象

对上述两个对象好好了解下,就差不多了。

#######写 Cookie

// URL

NSURL *url = @"http://www.test.com";

// propertiesDic,可特殊设置, 改变其也是看这里

NSDictionary *propertiesDic = @{

NSHTTPCookieName: @"testName",

NSHTTPCookieValue:@"testValue",

NSHTTPCookieDomain: url.host,

NSHTTPCookiePath: url.path

};

NSHTTPCookie *cookie = [[NSHTTPCookie alloc] initWithProperties: propertiesDic];

// 设置 Cookie

[[NSHTTPCookieStorage sharedHTTPCookieStorage]setCookie:cookie];

NSHTTPCookie 可中可改变的属性,特变注意前面几个。

FOUNDATION_EXPORT NSHTTPCookiePropertyKey const NSHTTPCookieName;

FOUNDATION_EXPORT NSHTTPCookiePropertyKey const NSHTTPCookieValue;

FOUNDATION_EXPORT NSHTTPCookiePropertyKey const NSHTTPCookieOriginURL;

FOUNDATION_EXPORT NSHTTPCookiePropertyKey const NSHTTPCookieVersion;

FOUNDATION_EXPORT NSHTTPCookiePropertyKey const NSHTTPCookieDomain;

FOUNDATION_EXPORT NSHTTPCookiePropertyKey const NSHTTPCookiePath;

FOUNDATION_EXPORT NSHTTPCookiePropertyKey const NSHTTPCookieSecure;

FOUNDATION_EXPORT NSHTTPCookiePropertyKey const NSHTTPCookieExpires;

FOUNDATION_EXPORT NSHTTPCookiePropertyKey const NSHTTPCookieComment;

FOUNDATION_EXPORT NSHTTPCookiePropertyKey const NSHTTPCookieCommentURL;

FOUNDATION_EXPORT NSHTTPCookiePropertyKey const NSHTTPCookieDiscard;

FOUNDATION_EXPORT NSHTTPCookiePropertyKey const NSHTTPCookieMaximumAge;

FOUNDATION_EXPORT NSHTTPCookiePropertyKey const NSHTTPCookiePort;

#######读 Cookie

NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];

for (NSHTTPCookie *cookie in [cookieStorage cookies]) {

NSLog(@"%@", cookie);

}

#######清空 Cookie

NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];

for (NSHTTPCookie *cookie in [cookieStorage cookies]) {

[cookieStorage deleteCookie: cookie];

}

说白了就是下面属性 和 方法的运用:

/*!

@method cookies

@abstract Get all the cookies

@result An NSArray of NSHTTPCookies

*/

@property (nullable , readonly, copy) NSArray *cookies;

/*!

@method setCookie:

@abstract Set a cookie

@discussion The cookie will override an existing cookie with the

same name, domain and path, if any.

*/

- (void)setCookie:(NSHTTPCookie *)cookie;

/*!

@method deleteCookie:

@abstract Delete the specified cookie

*/

- (void)deleteCookie:(NSHTTPCookie *)cookie;

整体来说,此次是更多的是 Cookie 和 负载相关的了解,对 iOS 之外的更多理解才是重点,也是需要额外注意的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值