gpt4 book ai didi

swift - 如何在 Swift 中从 URLSession 响应中获取 cookie

转载 作者:行者123 更新时间:2023-11-30 10:39:31 25 4
gpt4 key购买 nike

这是我第一次提问。

我正在使用 Swift 5。我正在做一个登录功能,所以我需要使用Cookie保存“JSESSIONID”,所以我试图从响应头的Set-Cookie参数中读取数据,但我无法获取任何数据。如何获取数据?

我检查了“Set-Cookie”是由servlet 使用curl 命令发送的。我附加了一个响应 header 。

jun-MacBook-Pro:~ junki-t$ curl -X POST -D - -d "SOME PARAMETERS" http://example.com/PATH/UserProfilingService
HTTP/1.1 302 Found
Date: Tue, 23 Jul 2019 10:49:02 GMT
Set-Cookie: JSESSIONID=91********************BA; Path=/******; HttpOnly
Location: /?AUTHORIZED=yes&USERPROFILEKEY=*********
Content-Length: 0
Connection: close
Content-Type: text/plain

jun-MacBook-Pro:~ junki-t$

我尝试在 Swift 应用程序中转储响应数据,但未找到“Set-Cookie”。

- some: <NSHTTPURLResponse: 0x281050860> { URL: http://example.com/?AUTHORIZED=yes&USERPROFILEKEY=********* } { Status Code: 200, Headers {
"Accept-Ranges" = (
bytes
);
Connection = (
close
);
"Content-Length" = (
146
);
"Content-Type" = (
"text/html"
);
Date = (
"Wed, 17 Jul 2019 08:51:38 GMT"
);
Etag = (
"\"92-5********a3\""
);
"Last-Modified" = (
"Wed, 30 Nov 2016 07:44:17 GMT"
);
Server = (
Apache
);
} } #0
- super: NSURLResponse
- super: NSObject
func tryAuth() {
let id = "*****"
let pass = "*****"

let url = URL(string: "<Some url>")
var url_request = URLRequest(url: url!)
url_request.httpMethod = "POST"
url_request.httpShouldHandleCookies = true

let body = "SOME PARAMETERS FOR AUTHENTICATE"
url_request.httpBody = body.data(using: String.Encoding.utf8)

let config = URLSessionConfiguration.default
let session = URLSession(configuration: config, delegate: self, delegateQueue: nil)
session.configuration.httpCookieAcceptPolicy = .always
session.configuration.httpCookieStorage = HTTPCookieStorage.shared
session.configuration.httpShouldSetCookies = true

session.dataTask(with: url_request).resume()
}
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse, completionHandler: @escaping (URLSession.ResponseDisposition) -> Void) {
let httpResponse = response as? HTTPURLResponse
var dict = httpResponse?.allHeaderFields
let cookies = HTTPCookie.cookies(withResponseHeaderFields: httpResponse?.allHeaderFields as! [String : String], for: response.url!)
dump(cookies) // for testing.
//
// some process to get/save cookie.
//
}

我认为 cookie 有一些数据(我想获取“JSESSIONID”),但 cookie 有 0 个元素。

所以我无法获取任何cookie数据。

最佳答案

来自 HTTPCookieAcceptPolicy 文档:

This property determines the cookie accept policy for all tasks within sessions based on this configuration. The default value is NSHTTPCookieAcceptPolicyOnlyFromMainDocumentDomain. You can change it to any of the constants defined in the NSHTTPCookieAcceptPolicy enumerated type. If you want more direct control over what cookies are accepted, set this value to NSHTTPCookieAcceptPolicyNever and then use the allHeaderFields and cookiesWithResponseHeaderFields:forURL: methods to extract cookies from the URL response object yourself.

因此,您需要设置 session.configuration.httpCookieAcceptPolicy = .never 或者您可以根据需要使用 HTTPCookieStorage.shared.cookies(for: response.url)系统自动设置cookie。

关于swift - 如何在 Swift 中从 URLSession 响应中获取 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57166288/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com