gpt4 book ai didi

ios - 通过请求位于 URL 的文件/网页的 header 获取 MIMETYPE

转载 作者:搜寻专家 更新时间:2023-11-01 06:19:56 29 4
gpt4 key购买 nike

我需要知道文件网页MIMEType,可在特定的URL处获得并根据MIMEType 我将决定是否将该页面/文件加载到 UIWebView 中。我知道,我可以使用 NSURLResponse 对象获取 MIMEType,但问题是,当我收到此响应时,该页面已经下载了。

那么,有没有办法只询问特定URL 处的文件/网页 的 header ,以便我可以检查MIMEType并仅在需要时请求该页面/文件?

最佳答案

您可以使用响应 header 的“Content-Type”属性获取内容类型:

func getContentType(urlPath: String, completion: (type: String)->()) {
if let url = NSURL(string: urlPath) {
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "HEAD"
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { (_, response, error) in
if let httpResponse = response as? NSHTTPURLResponse where error == nil {
if let ct = httpResponse.allHeaderFields["Content-Type"] as? String {
completion(type: ct)
}
}
}
task.resume()
}
}

getContentType("http://example.com/pic.jpg") { (type) in
print(type) // prints "image/jpeg"
}

关于ios - 通过请求位于 URL 的文件/网页的 header 获取 MIMETYPE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35767694/

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