Here you can find the source of constructURL(URL base, String url, boolean stripRef)
Parameter | Description |
---|---|
base | The base URL. |
url | The URL that was found on the base URL's page. |
stripRef | True if the URL's reference should be stripped. |
Parameter | Description |
---|---|
IOException | Thrown if any IO error occurs. |
public static URL constructURL(URL base, String url, boolean stripRef) throws IOException
//package com.java2s; /**// w ww . j a v a2 s .co m * The Heaton Research Spider Copyright 2007 by Heaton * Research, Inc. * * HTTP Programming Recipes for Java ISBN: 0-9773206-6-9 * http://www.heatonresearch.com/articles/series/16/ * * URLUtility: A set of useful utilities for processing * URL's. * * This class is released under the: * GNU Lesser General Public License (LGPL) * http://www.gnu.org/copyleft/lesser.html * * @author Jeff Heaton * @version 1.1 */ import java.io.*; import java.net.*; public class Main { /** * Construct a URL from its basic parts. * @param base The base URL. * @param url The URL that was found on the base URL's page. * @param stripRef True if the URL's reference should be stripped. * @return The new URL, built upon the base URL. * @throws IOException Thrown if any IO error occurs. */ public static URL constructURL(URL base, String url, boolean stripRef) throws IOException { URL result = new URL(base, url); String file = result.getFile(); String protocol = result.getProtocol(); String host = result.getHost(); int port = result.getPort(); String ref = result.getRef(); StringBuilder sb = new StringBuilder(file); int index = sb.indexOf(" "); while (index != -1) { if (index != -1) { sb.replace(index, index + 1, "%20"); } index = sb.indexOf(" "); } file = sb.toString(); if ((ref != null) && !stripRef) { result = new URL(protocol, host, port, file + "#" + ref); } else { result = new URL(protocol, host, port, file); } return result; } }