List of usage examples for java.net URL getQuery
public String getQuery()
From source file:org.eclipse.skalli.commons.URLUtils.java
/** * Converts a given string into a corresponding URL. * <p>// w w w. j av a 2 s . c o m * Encodes path and/or query parts of the given string according to * {@link URI#URI(String, String, String, int, String, String, String)}. * For example, blanks in the path are converted to <tt>%20</tt>. * * @param s the string to convert to an URL. * @return an URL, or <code>null</code> if the string is <code>null</code>, empty or whitespace. * * @throws MalformedURLException if the given string is not a valid URL and cannot be * "sanitized" to yield a valid URL even after proper encoding of its parts. */ public static URL stringToURL(String s) throws MalformedURLException { if (StringUtils.isBlank(s)) { return null; } URI uri = null; try { uri = new URI(s); } catch (URISyntaxException e) { URL url = new URL(s); try { uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef()); } catch (URISyntaxException e1) { MalformedURLException e2 = new MalformedURLException(e1.getMessage()); e2.initCause(e1); throw e2; } } return new URL(uri.toASCIIString()); }
From source file:com.flipkart.poseidon.handlers.http.utils.StringUtils.java
/** * Gets the query parameters from the URL * @param httpUrl The URL from which query params have to be extracted * @return /*from ww w .ja va 2s . c om*/ */ public static Map<String, String> getQueryParams(String httpUrl) { Map<String, String> params = new HashMap<String, String>(); if (httpUrl == null) { return params; } URL url = null; try { url = new URL(httpUrl); } catch (MalformedURLException e) { throw new RuntimeException(e); } String query = url.getQuery(); if (query == null) { return params; } StringTokenizer tokenizer = new StringTokenizer(query, "&"); while (tokenizer.hasMoreTokens()) { String token = tokenizer.nextToken(); int index = token.indexOf("="); params.put(token.substring(0, index).trim(), token.substring(index + 1).trim()); } return params; }
From source file:org.motechproject.mmnaija.web.util.HTTPCommunicator.java
public static String doGet(String urlStr) { URLConnection connection = null; try {/*from ww w. j a v a2 s . c o m*/ URL url = new URL(urlStr); URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef()); url = uri.toURL(); // Open the connection connection = url.openConnection(); } catch (Exception e) { } try { InputStream in = connection.getInputStream(); StringWriter writer = new StringWriter(); IOUtils.copy(in, writer, "utf-8"); String theString = writer.toString(); return theString; } catch (Exception e) { } return ""; }
From source file:com.familygraph.android.Util.java
/** * Parse a URL query and fragment parameters into a key-value bundle. * /*from www.j av a2 s. c o m*/ * @param url * the URL to parse * @return a dictionary bundle of keys and values */ public static Bundle parseUrl(String url) { // hack to prevent MalformedURLException url = url.replace("fgconnect", "http"); try { URL u = new URL(url); Bundle b = decodeUrl(u.getQuery()); b.putAll(decodeUrl(u.getRef())); return b; } catch (MalformedURLException e) { return new Bundle(); } }
From source file:com.predic8.membrane.core.util.HttpUtil.java
public static String getPathAndQueryString(String dest) throws MalformedURLException { URL url = new URL(dest); String uri = url.getPath();//from ww w . ja v a 2s . c om if (url.getQuery() != null) { return uri + "?" + url.getQuery(); } return uri; }
From source file:org.talend.commons.utils.network.NetworkUtil.java
/** * encode url//ww w . j ava 2 s .com * * @param urlStr url not encoded yet! * @return * @throws Exception */ public static URL encodeUrl(String urlStr) throws Exception { try { // String decodedURL = URLDecoder.decode(urlStr, "UTF-8"); //$NON-NLS-1$ URL url = new URL(urlStr); URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef()); return uri.toURL(); } catch (Exception e) { throw e; } }
From source file:de.fhg.iais.asc.xslt.binaries.download.Downloader.java
private static URI createURI(URL url) throws URISyntaxException { return new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef()); }
From source file:com.geozen.demo.foursquare.jiramot.Util.java
/** * Parse a URL query and fragment parameters into a key-value bundle. * /* w w w . j a va 2 s. c o m*/ * @param url * the URL to parse * @return a dictionary bundle of keys and values */ public static Bundle parseUrl(String url) { // hack to prevent MalformedURLException url = url.replace("#", "?"); try { URL u = new URL(url); Bundle b = decodeUrl(u.getQuery()); // b.putAll(decodeUrl(u.getRef())); return b; } catch (MalformedURLException e) { return new Bundle(); } }
From source file:org.motechproject.mmnaija.web.util.HTTPCommunicator.java
public static String doPost(String serviceUrl, String queryString) { URLConnection connection = null; try {/*from w w w.j a v a2 s .c o m*/ URL url = new URL(serviceUrl); URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef()); url = uri.toURL(); // Open the connection connection = url.openConnection(); connection.setDoInput(true); connection.setUseCaches(false); // Disable caching the document connection.setDoOutput(true); // Triggers POST. connection.setRequestProperty("Content-Type", "text/html"); OutputStreamWriter writer = null; log.info("About to write"); try { if (null != connection.getOutputStream()) { writer = new OutputStreamWriter(connection.getOutputStream()); writer.write(queryString); // Write POST query } else { log.warn("connection Null"); } // string. } catch (ConnectException ex) { log.warn("Exception : " + ex); // ex.printStackTrace(); } finally { if (writer != null) { try { writer.close(); } catch (Exception lg) { log.warn("Exception lg: " + lg.toString()); //lg.printStackTrace(); } } } InputStream in = connection.getInputStream(); // StringWriter writer = new StringWriter(); IOUtils.copy(in, writer, "utf-8"); String theString = writer.toString(); return theString; } catch (Exception e) { //e.printStackTrace(); log.warn("Error URL " + e.toString()); return ""; } }
From source file:luis.clientebanco.OAuth.Utils.java
/** * Parse a URL query and fragment parameters into a key-value bundle. * /*ww w .j av a 2 s . c om*/ * @param url the URL to parse * @return a dictionary bundle of keys and values */ public static Bundle parseUrl(String url) { // hack to prevent MalformedURLException url = url.replace("fbconnect", "http"); url = url.replace("moneyvault", "http"); try { URL u = new URL(url); Bundle b = decodeUrl(u.getQuery()); b.putAll(decodeUrl(u.getRef())); return b; } catch (MalformedURLException e) { return new Bundle(); } }