Here you can find the source of buildUrl(String host, int port, String path, Map
Parameter | Description |
---|---|
host | The host |
port | The port |
path | The path |
parameters | The parameters |
Parameter | Description |
---|---|
MalformedURLException | an exception |
public static URL buildUrl(String host, int port, String path, Map<String, String> parameters) throws MalformedURLException
//package com.java2s; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder; import java.util.Map; public class Main { public static final String UTF8 = "UTF-8"; /**/*www . j a v a2 s . com*/ * Build a request URL. * * @param host * The host * @param port * The port * @param path * The path * @param parameters * The parameters * @return The URL * @throws MalformedURLException */ public static URL buildUrl(String host, int port, String path, Map<String, String> parameters) throws MalformedURLException { // see: AuthUtilities.getSignature() // AuthUtilities.addAuthToken(parameters); StringBuffer buffer = new StringBuffer(); if (!host.startsWith("http://")) { buffer.append("http://"); } buffer.append(host); if (port > 0) { buffer.append(':'); buffer.append(port); } if (path == null) { path = "/"; } buffer.append(path); if (!parameters.isEmpty()) { buffer.append('?'); } int size = parameters.size(); for (Map.Entry<String, String> entry : parameters.entrySet()) { buffer.append(entry.getKey()); buffer.append('='); Object value = entry.getValue(); if (value != null) { String string = value.toString(); try { string = URLEncoder.encode(string, UTF8); } catch (UnsupportedEncodingException e) { // Should never happen, but just in case } buffer.append(string); } if (--size != 0) { buffer.append('&'); } } /* * RequestContext requestContext = RequestContext.getRequestContext(); Auth auth = requestContext.getAuth(); if (auth != null && * !ignoreMethod(getMethod(parameters))) { buffer.append("&api_sig="); buffer.append(AuthUtilities.getSignature(sharedSecret, parameters)); } */ return new URL(buffer.toString()); } }