Java examples for Network:URL
Build a fully qualified URL from the pieces provided.
/**/*from w w w. j a v a 2s. c o m*/ * Copyright (c) 2000-2016 Liferay, Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ //package com.java2s; public class Main { /** * Build a fully qualified URL from the pieces provided. This method does not call any encoding methods, so the path * argument has to be properly encoded. * * @param scheme server protocol. * @param host server name. * @param port server port. * @param path properly encoded relative URL. * * @throws IllegalArgumentException if scheme, host or port is null. */ public static String buildUrlAsString(String scheme, String host, int port, String path) { if ((scheme == null) || scheme.equals("") || (host == null) || host.equals("") || (port == 0)) { throw new IllegalArgumentException( "Cannot build a URL using following scheme: " + scheme + " host: " + host + " port: " + port + " path: " + path); } StringBuilder url = new StringBuilder(200); url.append(scheme).append("://").append(host); // check for protocol default port number if ((scheme.equalsIgnoreCase("http") && (port != 80)) || (scheme.equalsIgnoreCase("https") && (port != 443))) { url.append(":").append(port); } if (path != null) { url.append(path); } return url.toString(); } }