Java tutorial
//package com.java2s; //License from project: Open Source License public class Main { static final public String HTTP_PROTOCOL = "http://"; static final public String HTTPS_PROTOCOL = "https://"; static public String computeEffectiveServerUrl(String serverUrl, String userName, String password) throws Exception { if (null == userName && null != password) { throw new Exception("Can not specify a password without a user name"); } if (null != userName && null == password) { throw new Exception("Can not specify a user name without a password"); } if (null != userName && null == serverUrl) { throw new Exception("Can not specify a user name/password without a server URL"); } if (null == serverUrl) { return ""; } String serverUrlWithoutProtocol = null; String protocol = null; if (serverUrl.startsWith(HTTP_PROTOCOL)) { protocol = HTTP_PROTOCOL; serverUrlWithoutProtocol = serverUrl.substring(HTTP_PROTOCOL.length()); } else if (serverUrl.startsWith(HTTPS_PROTOCOL)) { protocol = HTTPS_PROTOCOL; serverUrlWithoutProtocol = serverUrl.substring(HTTPS_PROTOCOL.length()); } else { throw new Exception("Server URL must specify a protocol (http:// or https://)"); } if (null != userName) { serverUrl = protocol + userName + ":" + password + "@" + serverUrlWithoutProtocol; } return serverUrl; } }