swift3设置cookie

代码
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
swift3设置cookie_第1张图片
image.png
这是因为 HTTPCookiePropertyKey.domain 值设置的不对,应该如下设置:

cookieProperties[HTTPCookiePropertyKey.domain] = "xxxx.com" as String

swift3设置cookie_第2张图片
image.png
问过安卓哥哥那边,他们带了http没问题。应该就是规范不一样吧。

记一坑。

你可能感兴趣的:(swift3设置cookie)