Java URL Create concatenateURL(final URL url, final String query)

Here you can find the source of concatenateURL(final URL url, final String query)

Description

Concatenates the given java.net.URL and query.

License

Open Source License

Parameter

Parameter Description
url URL to base off
query Query to append to URL

Return

URL constructed

Declaration

public static URL concatenateURL(final URL url, final String query) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.net.MalformedURLException;
import java.net.URL;

public class Main {
    /**//from  w  w w.j  av  a  2s . c  o  m
     * Concatenates the given {@link java.net.URL} and query.
     *
     * @param url   URL to base off
     * @param query Query to append to URL
     * @return URL constructed
     */
    public static URL concatenateURL(final URL url, final String query) {
        try {
            if (url.getQuery() != null && url.getQuery().length() > 0) {
                return new URL(url.getProtocol(), url.getHost(), url.getPort(), url.getFile() + "&" + query);
            } else {
                return new URL(url.getProtocol(), url.getHost(), url.getPort(), url.getFile() + "?" + query);
            }
        } catch (final MalformedURLException ex) {
            throw new IllegalArgumentException("Could not concatenate given URL with GET arguments!", ex);
        }
    }
}

Related

  1. buildURL(URI base, Multimap params)
  2. buildUrlPath(String baseUrl, String childUrl)
  3. buildUrlsList(final String domain, final String... paths)
  4. buildURLString(Iterable elements, String joiner)
  5. concatenate(URL server, String address)
  6. concatenateURL(URL url, String query)
  7. concatUrl(final URL baseUrl, final String extraPath)
  8. concaturl(final URL p_base, final String p_string)
  9. constructURL(URL base, String url, boolean stripRef)