代码
var cookieProperties = [HTTPCookiePropertyKey: String]()
cookieProperties[HTTPCookiePropertyKey.name] = "key" as String
cookieProperties[HTTPCookiePropertyKey.value] = "value" as String
cookieProperties[HTTPCookiePropertyKey.domain] = "http://xxxx.com" as String
cookieProperties[HTTPCookiePropertyKey.path] = "/" as String
let cookie = HTTPCookie(properties: cookieProperties)
HTTPCookieStorage.shared.setCookie(cookie!)
发现输出时有值
let cookieArray = HTTPCookieStorage.shared.cookies!
for cookie in cookieArray
{
print("name:\(cookie.name),value:\(cookie.value)")
}
name:JSESSIONID,value:A695DD783ACCA2355AF7B9005AF4B3A5
name:key,value:value
但是抓包时没有自己设置的cookie
image.png
这是因为 HTTPCookiePropertyKey.domain 值设置的不对,应该如下设置:
cookieProperties[HTTPCookiePropertyKey.domain] = "xxxx.com" as String
image.png