Code examples using HTTPS
You already understand how HTTPS works theoretically, but how can an Android developer use secure connections using HTTPS?
To establish an HTTP connection, all you need to do is run the following three lines of code:
URL url = new URL("http://wikipedia.org"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); InputStream in = connection.getInputStream();
Wikipedia supports secure communications, so let's change the code to make it use HTTPS instead of HTTP, as shown in the following code:
URL url = new URL("https://wikipedia.org"); HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); InputStream in = connection.getInputStream();
Can you see the difference? Well, if you can see the difference, congratulations! You have a very sharp eye. If you can't, here is a little hint: check the protocol in the URL again and the HttpURLConnection
class. Now you see the little s after http in the URL and in the class name, and yes, that is...