我目前正在使用SwiftHTTP来获取url地址的来源。我使用'get method'来获取url地址的源代码
do {
let opt = try HTTP.GET(self.my_url_address!)
opt.start { response in
if let err = response.error {
print("error: \(err.localizedDescription)")
return
}
print(response.description)
}
} catch let error {
print("got an error creating the request: \(error)")
}
在此代码运行之后,我在Xcode输出屏幕中获得了此输出
URL: http://myweburl.com/detay/swat-under-siege.html
Status Code: 200
Headers: Content-Type: text/html
Connection: keep-alive
CF-RAY: 38391215a60e2726-FRA
Set-Cookie: ASPSESSIONIDSABBBSDT=HPKKPJGCDLKMDMILNGHPCAGD; path=/
Date: Mon, 24 Jul 2017 18:51:24 GMT
Vary: Accept-Encoding
X-Powered-By: ASP.NET Transfer-Encoding: Identity
Server: cloudflare-nginx
Content-Encoding: gzip
Cache-Control: private
状态代码为200,但输出不是url的源代码。我该如何解决这个问题?
答案 0 :(得分:1)
回应是正确的。我尝试过请求网站(真实网站)并且有效:
print(response.data.base64EncodedString())
如果您解码BASE64数据,它将呈现有效的HTML代码。
问题似乎与编码有关。检查网站的主标记后,它会指出字符集为windows-1254
String(data: response.data, encoding: .windowsCP1254) // works. latin1, etc.
类似