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.


package com.google.test;

/**

* To compiple: javac -d . ConvertHttpToHttps.java

* To run: java com.google.test.ConvertHttpToHttps

*

* @author ashik

*/
public final class ConvertHttpToHttps {

private static String url = "http://configure.google.com:80/AppStore/en/US/enterpriseMgr/AppStore";

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);
result.append("https://");
int colonIndex = strColon.indexOf(":");
String portNumber = strColon.substring(0, colonIndex);
result.append(portNumber);
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:

WordPress.com Logo

You are commenting using your WordPress.com 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.