【问题标题】:How to read Content-Length HTTPResponse with Alamofire Swift如何使用 Alamofire Swift 读取 Content-Length HTTPResponse
【发布时间】:2020-05-21 08:47:09
【问题描述】:

我正在尝试从响应中读取Content-Length,但它总是跳过并且没有得到响应。即使是最简单的。我使用不同的 URL 链接进行检查,但每次跳过的结果都是相同的,并且没有响应。

  • 甚至可以使用任何URL链接功能跳过。

网址链接: 计算长度(链接:“”)

代码:

func calculateLength(Link: String)  {
        Alamofire.request(Link, method: .get, parameters: nil, encoding: URLEncoding.httpBody).responseJSON
                    { response in
                        //to get JSON return value
                        if let ALLheader = response.response?.allHeaderFields  {
                            if let header = ALLheader as? [String : Any] {
                                if let contentLength = header["Content-Length"] as? String {

                                }
                            }
                        }
        }
    }

从服务器获取响应:

(Response) <NSHTTPURLResponse: 0x6000000c7120> { URL:  } { Status Code: 200, Headers {
    "Cache-Control" =     (
        private
    );
    "Content-Encoding" =     (
        gzip
    );
    "Content-Length" =     (
        837
    );
    "Content-Type" =     (
        "text/html"
    );
    Date =     (
        "Thu, 21 May 2020 08:04:55 GMT"
    );
    Server =     (
        "Microsoft-IIS/8.5"
    );
    "Set-Cookie" =     (
        "ASPSESSIONIDSSSCCBAB=AKDPKPMCKNDJPANFODFAJKAH; path=/"
    );
    Vary =     (
        "Accept-Encoding"
    );
    "X-Powered-By" =     (
        "ASP.NET"
    );
} }

【问题讨论】:

  • 可能是因为 Content-Length 是 Int,所以你的演员 as? String 失败了?
  • @serg_zhd 即使我在if let ALLheader = response.response?.allHeaderFields 上设置断点,它仍然会跳过并且响应不来不知道
  • 尝试从请求中删除 .responseJSON - 你得到的是空的正文,并且响应字段由于某种原因被设置为 nil
  • @serg_zhd 试过但没用,它跳过了。即使我使用了不同的网址

标签: ios swift alamofire httpresponse


【解决方案1】:

对于 Alamofire 5,您的函数如下所示:

func calculateLength(link: String) {
     AF.request(link, method: .get).response() { response in
         if let headers = response.response?.headers  {
             print(headers.value(for: "Content-Length"))
         }
     }
}

对于 Alamofire 4,它会是这样的

func calculateLength(link: String) {
     Alamofire.request(link, method: .get).responseJSON { response in
         if let headers = response.response?.allHeaderFields as? [String : Any] {
             if let contentLength = headers["Content-Length"] as? String {
                 print(contentLength)
             }
         }
     }
}

【讨论】:

    【解决方案2】:

    根据 iOS 版本访问HTTPURLResponse 的属性value(适用于iOS 13 及更高版本)或allHeaderFields。 您只需传递密钥名称,在您的情况下为 "Content-Length"

    extension HTTPURLResponse {
        func valueForHeaderField(_ headerField: String) -> String? {
            if #available(iOS 13.0, *) {
                return value(forHTTPHeaderField: headerField)
            } else {
                return (allHeaderFields as NSDictionary)[headerField] as? String
            }
        }
    }
    

    你可以像这样调用上面的函数,

    let contentLegth = response.response.valueForHeaderField("Content-Length")
    

    【讨论】:

      猜你喜欢
      • 2017-02-12
      • 1970-01-01
      • 2015-09-04
      • 1970-01-01
      • 1970-01-01
      • 2016-09-30
      • 2017-05-07
      • 2015-10-10
      • 1970-01-01
      相关资源
      最近更新 更多