0

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?

3
  • The documentation mentions print("data is: \(response.data)"). Commented Jul 24, 2017 at 19:16
  • the output is : "data is: 27905 bytes". I try let responseString = String(data: response.data, encoding: .utf8); which is doesn't work the output is only "nil" Commented Jul 24, 2017 at 19:19
  • Try using a plain old URLSession and check if the problem persists.. Commented Jul 24, 2017 at 19:47

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

String(data: response.data, encoding: .windowsCP1254) is the answer!. Thank you very much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.