2

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!

TheAlmac2
  • 193
  • 2
  • 15
  • 1
    `HttpUrlConnection` could handle both, http and https connection. – Anggrayudi H Apr 18 '18 at 13:42
  • 1
    Possible duplicate of [In java, how to create HttpsURLConnection or HttpURLConnection based on the url?](https://stackoverflow.com/questions/945003/in-java-how-to-create-httpsurlconnection-or-httpurlconnection-based-on-the-url) – Jan B. Apr 18 '18 at 13:45
  • @AnggrayudiH It's rather the other way round, since ``HttpsUrlConnection`` extends ``HttpUrlConnection``. – Jan B. Apr 18 '18 at 13:46
  • @Matt does this explain the error I am getting under the post below? thanks – TheAlmac2 Apr 18 '18 at 14:22

1 Answers1

4

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.

AxiomaticNexus
  • 6,190
  • 3
  • 41
  • 61
  • great! I seen that on the documentation but I thought I might need to overwrite the SSL socket layer and other things.. thanks! – TheAlmac2 Apr 18 '18 at 13:54
  • actually received this error : V/ErrorAPP: java.lang.ClassCastException: com.android.okhttp.internal.huc.HttpURLConnectionImpl cannot be cast to javax.net.ssl.HttpsURLConnection. – TheAlmac2 Apr 18 '18 at 14:18
  • HttpsURLConnection con = (HttpsURLConnection) object.openConnection(); ... this is how I have set up my object.. any ideas? – TheAlmac2 Apr 18 '18 at 14:19