Ashik’s IT Thoughts

March 15, 2010

Convert Http URL to Https URL – A Short Sample Code

Filed under: Java, Web — Tags: , , , , — ashikuzzaman @ 7:31 pm

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();
}

}

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: