来自HTTPURLResponse的重复Set-Cookie标头

时间:2017-01-11 00:55:45

标签: nshttpcookie nshttpurlresponse

我正在使用API​​来获取用于验证后续请求的cookie。我需要从这个响应标头集合中获取cookie:

{
    "Server": "Cowboy",
    "Connection": "close",
    "Set-Cookie": "heroku-session-affinity=ADaDaANoA24IAXtpQOD///8HYgAAKehiAA6V12ECbAAAAAJtAAAABXdlYi4ybQAAAAV3ZWIuMWrNqkap0u4H0uG3Btrtlamaq2nQ5w__; Version=1; Expires=Wed, 11-Jan-2017 01:12:09 GMT; Max-Age=86400; Domain=xxxxxxxx.xxxx.xx; Path=/; HttpOnly",
    "X-Powered-By": "Express",
    "Vary": "Origin",
    "Access-Control-Allow-Credentials": "true",
    "Content-Type": "application/json; charset=utf-8",
    "Content-Length": "30914",
    "Etag": "W/\"78c2-veX2kgrO3zh118nYsOP80A\"",
    "Set-Cookie": "connect.sid=s%3ACX3tfhPk9nlRtU8e8gtouQIx1kpP07h9.iExm%2F96Dgzuh289nmjwbYO49E0Bq0WwUHmN539IkudI; Path=/; Expires=Thu, 12 Jan 2017 01:12:09 GMT; HttpOnly",
    "Date": "Tue, 10 Jan 2017 01:12:09 GMT",
    "Via": "1.1 vegur"
}

我需要获得以下cookie:

"connect.sid=s%3ACX3tfhPk9nlRtU8e8gtouQIx1kpP07h9.iExm%2F96Dgzuh289nmjwbYO49E0Bq0WwUHmN539IkudI; Path=/; Expires=Thu, 12 Jan 2017 01:12:09 GMT; HttpOnly"

我想解析标题并返回该Cookie,但以下代码仅显示我不想要的Cookie:

internal final class CookieParser {

    internal static func cookie(from response: HTTPURLResponse) -> HTTPCookie? {

        print("\n====> Response: \(response)\n")

        guard let url = response.url else { return nil }

        guard let headerFields = response.allHeaderFields as? HTTPHeaders else { return nil }

        print("\n====> Headers: \(headerFields)\n")

        let cookies = HTTPCookie.cookies(withResponseHeaderFields: headerFields, for: url)

        print("\n====> Cookies: \(cookies)\n")

        return nil
    }

}

上面的代码产生以下输出:

====> Response: <NSHTTPURLResponse: 0x60800002e160> { URL: http://xxxxxxxx.xxxx.xx/xxx/xx/xxxxx } { status code: 200, headers {
    "Access-Control-Allow-Credentials" = true;
    Connection = close;
    "Content-Length" = 30914;
    "Content-Type" = "application/json; charset=utf-8";
    Date = "Tue, 10 Jan 2017 01:12:09 GMT";
    Etag = "W/\"78c2-veX2kgrO3zh118nYsOP80A\"";
    Server = Cowboy;
    "Set-Cookie" = "heroku-session-affinity=ADaDaANoA24IAXtpQOD///8HYgAAKehiAA6V12ECbAAAAAJtAAAABXdlYi4ybQAAAAV3ZWIuMWrNqkap0u4H0uG3Btrtlamaq2nQ5w__; Version=1; Expires=Wed, 11-Jan-2017 01:12:09 GMT; Max-Age=86400; Domain=partners.flye.co; Path=/; HttpOnly";
    Vary = Origin;
    Via = "1.1 vegur";
    "X-Powered-By" = Express;
} }


====> Headers: ["Content-Type": "application/json; charset=utf-8", "Content-Length": "30914", "Connection": "close", "Set-Cookie": "heroku-session-affinity=ADaDaANoA24IAXtpQOD///8HYgAAKehiAA6V12ECbAAAAAJtAAAABXdlYi4ybQAAAAV3ZWIuMWrNqkap0u4H0uG3Btrtlamaq2nQ5w__; Version=1; Expires=Wed, 11-Jan-2017 01:12:09 GMT; Max-Age=86400; Domain=partners.flye.co; Path=/; HttpOnly", "Vary": "Origin", "Etag": "W/\"78c2-veX2kgrO3zh118nYsOP80A\"", "Access-Control-Allow-Credentials": "true", "X-Powered-By": "Express", "Date": "Tue, 10 Jan 2017 01:12:09 GMT", "Via": "1.1 vegur", "Server": "Cowboy"]


====> Cookies: [<NSHTTPCookie version:1 name:"heroku-session-affinity" value:"ADaDaANoA24IAXtpQOD///8HYgAAKehiAA6V12ECbAAAAAJtAAAABXdlYi4ybQAAAAV3ZWIuMWrNqkap0u4H0uG3Btrtlamaq2nQ5w__" expiresDate:2017-01-12 00:41:08 +0000 created:2017-01-11 00:41:08 +0000 sessionOnly:FALSE domain:".partners.flye.co" partition:"none" path:"/" isSecure:FALSE>]

不需要的Set-Cookie值必须替换返回标头字典中的所需Set-Cookie值。有没有办法可以在替换之前检索所需的Set-Cookie值?

0 个答案:

没有答案