加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

从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获取标头

正如苹果文档所说:

A dictionary containing all the HTTP header fields received as part of the server’s response. By examining this dictionary clients can see the “raw” header information returned by the HTTP server.

The keys in this dictionary are the header field names,as received from the server. See RFC 2616 for a list of commonly used HTTP header fields.

因此,要获得响应标头中的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
     }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读