【发布时间】: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