gpt4 book ai didi

swift - request.allHTTPHeaderFields 总是返回 Optional([ :])

转载 作者:可可西里 更新时间:2023-10-31 23:57:30 30 4
gpt4 key购买 nike

我正在尝试获取网页的最后修改日期并尝试使用以下代码。但它总是在控制台中返回“Optional([:])”(空):

let url = URL(string:"http://www.carl-bechstein-gymnasium.de/news/news_index.htm")

let req = NSMutableURLRequest(url: url!, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 10.0)
req.httpMethod = "GET"
let headerFields = req.allHTTPHeaderFields
print(headerFields)

我还将此页面添加到 Info.plist 中:

<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>NSExceptionDomains</key>
<dict>
<key>http://www.carl-bechstein-gymnasium.de/</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>
</dict>

但是没有用。

所以我试着检查,如果有任何标题,并且有!这是 website 的摘录,我在哪里检查过:

Date:·Thu,·26·Oct·2017·20:15:42·GMT(CR)(LF) Content-Type:·text/html(CR)(LF) Content-Length:·36519(CR)(LF) Connection:·close(CR)(LF) Server:·Apache(CR)(LF) Last-Modified:·Wed,·18·Oct·2017·15:17:42·GMT(CR)(LF)

现在我完全糊涂了,希望尽快得到帮助...

最佳答案

您是否正在尝试获取网站请求或响应的 header ?现在您正在获取请求的所有 header ,由于您没有任何 header ,因此它会是空的。

如果您正在尝试获取响应 header ,您可以通过将其转换为 URLSession< 中返回的 HTTPURLResponse 对象来通过 URLResponse 获取这些 header 数据任务,像这样:

URLSession.shared.dataTask(with: req) { data, response, error in
if let httpResponse = response as? HTTPURLResponse {
print(httpResponse.allHeaderFields)
}
}.resume()

关于swift - request.allHTTPHeaderFields 总是返回 Optional([ :]),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46963167/

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