Antiokhos
Antiokhos

Reputation: 3045

can't get web page source from url in Swift

I'm currently using SwiftHTTP for getting source of url address. I am using 'get method' for getting source code of url address which is

 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)")
        }

after this code run I got this output in Xcode output screen

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

The status code is 200 but the output is not the source code of url. How can I fix this?

Upvotes: 0

Views: 153

Answers (1)

nathan
nathan

Reputation: 9385

Response is correct. I've tried requesting the website (the real one) and it works:

print(response.data.base64EncodedString())

If you decode the BASE64 data, it will render valid HTML code.

The issue seems related to encoding. After checking the website's head tag, it states that the charset is windows-1254

String(data: response.data, encoding: .windowsCP1254) // works. latin1, etc.

Your issue is similar to SWIFT: NSURLSession convert data to String

Upvotes: 1

Related Questions