Search code examples
javaandroidhttprequesthttpurlconnectionhttpsurlconnection

HttpUrlConnection to HttpsUrlConnection, can i just add the 's'?


I have developed an android application that is successfully sending data from my mobile application to my REST service and then to a database using httpurlconnection. However, I have been asked that I use httpsurlconnection as the REST service will be hosted in a secure location.

My requests include the url then opening a connection object. I have authorization headers and then I send through the data as a JSON object.

Can I simply add the 's' to the httpurlconnection or what other aspects do I need to think about?

Would appreciate the help, Thanks!


Solution

  • According to the Android documentation:

    Calling openConnection() on a URL with the "https" scheme will return an HttpsURLConnection

    So yes, you can simply cast your returned connection to HttpsURLConnection when calling openConnection(). In most cases there's nothing else you need to do; the SSL certificate, handshakes, and everything else related to the HTTPS protocol is handled by the system.