从swift中的请求响应中获取头数据
发布时间:2020-12-14 02:25:25 所属栏目:百科 来源:网络整理
导读:我想在一个标题请求中获得X-Dem-Auth,swift在我的应用程序中存储它. 看到回复: headers { "Content-Length" = 95; "Content-Type" = "application/json; charset=utf-8"; Date = "Fri,15 Apr 2016 08:01:58 GMT"; Server = "Apache/2.4.18 (Unix)"; "X-Dem-
我想在一个标题请求中获得X-Dem-Auth,swift在我的应用程序中存储它.
看到回复: headers { "Content-Length" = 95; "Content-Type" = "application/json; charset=utf-8"; Date = "Fri,15 Apr 2016 08:01:58 GMT"; Server = "Apache/2.4.18 (Unix)"; "X-Dem-Auth" = null; "X-Powered-By" = Express;
如果响应是NSHTTPURLResponse的类型,则可以从response.allHeaderFields获取标头
正如苹果文档所说:
因此,要获得响应标头中的X-Dem-Auth,您可以通过以下方式访问它: if let httpResponse = response as? NSHTTPURLResponse { if let xDemAuth = httpResponse.allHeaderFields["X-Dem-Auth"] as? String { // use X-Dem-Auth here } } UPDATE 由于Evan R的评论而更新 if let httpResponse = response as? HTTPURLResponse { if let xDemAuth = httpResponse.allHeaderFields["X-Dem-Auth"] as? String { // use X-Dem-Auth here } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |