Alamofire Swift get html source

自作多情 提交于 2019-12-22 08:13:22

问题


I just want to retrieve the html source from a simple website.

@IBAction func scan_func(sender: AnyObject) {
        Alamofire.request(.GET, "http://www.example.com")
            .response { request, response, data, error in
                print(request)
                print(response)
                print(data)
        }
    }

I already have successfully added "App Transport Security Settings" and "Allow Arbitrary Loads" to info.plist to load http content.

When I run that code I only get an output like this: XCODE - hexadecimal output I hope you can help me.

kind regards


回答1:


You are printing out the raw bytes of the data. I'm guessing you are looking for a way to print out the corresponding string representation. You could for instance do this with Alamofire's provided closure:

Alamofire.request(.GET, "http://www.example.com")
    .responseString { response in
        print("Response String: \(response.result.value)")
}

(Check out the documentation here)

Alternatively, you could convert the string yourself:

Alamofire.request(.GET, "http://www.example.com")
    .response { request, response, data, error in
        print(String(data: data, encoding: String.Encoding.utf8))
}

Apple String reference documentation



来源:https://stackoverflow.com/questions/38421011/alamofire-swift-get-html-source

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!