Convert Http URL to Https URL – A Short Sample Code

Here is an example of how you convert an Http URL to a secured URL (Https). Here I am assuming the URL will be fully standard i.e. even the default port 80 will be written after colon as :80 and same for default https port :443.



* To compiple: javac -d .

* To run: java


* @author ashik

public final class ConvertHttpToHttps {

private static String url = "";

public static void main (String[] args) {
System.out.println("url before replacing = " + url);
System.out.println("url after replacing = " + convert(url));

public static String convert(String url) {
StringBuffer result = new StringBuffer();
if(url.startsWith("http://")) {
String strColon = url.substring(7);
int colonIndex = strColon.indexOf(":");
String portNumber = strColon.substring(0, colonIndex);
result.append(":443" + strColon.substring(colonIndex+3));
return result.toString();


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.