List of usage examples for java.net URLEncoder encode
public static String encode(String s, Charset charset)
From source file:Main.java
public static Element appendSingleValElementEncoded(Document owner, Element appendElement, String newElemName, String newElemVal) {/*from w w w . j ava2s . c o m*/ Element newElem; newElem = owner.createElement(newElemName); if (newElemVal != null) { String encodedVal = ""; try { encodedVal = URLEncoder.encode(newElemVal, "UTF-8"); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } Text value = owner.createTextNode(encodedVal); newElem.appendChild(value); newElem.setAttribute("enc", "t"); newElem.setAttribute("charSet", "UTF-8"); } appendElement.appendChild(newElem); return (newElem); }
From source file:org.hammer.santamaria.mapper.dataset.CKAN2DataSetInput.java
/** * Encode URI/* w w w . j a va 2 s . c om*/ * * @param s * @return */ public static String EncodeURIComponent(String s) { String result; try { result = URLEncoder.encode(s, "UTF-8").replaceAll("\\+", "%20").replaceAll("\\%21", "!") .replaceAll("\\%27", "'").replaceAll("\\%28", "(").replaceAll("\\%29", ")") .replaceAll("\\%7E", "~"); } catch (UnsupportedEncodingException e) { result = s; } return result; }
From source file:org.eclipse.egit.github.core.util.UrlUtils.java
/** * Encode given url/*from ww w. j a va 2 s .c o m*/ * * @param url * @return encoded url */ public static String encode(String url) { try { return URLEncoder.encode(url, HTTP.DEFAULT_CONTENT_CHARSET); } catch (UnsupportedEncodingException e) { throw new IllegalArgumentException(e); } }
From source file:be.geecko.QuickLyric.utils.LastFMCorrection.java
public static String[] getCorrection(String artist, String track) { if ((artist == null) || (track == null)) return new String[] { null, null }; String encodedArtist;// w w w . ja va 2 s. co m String encodedSong; try { encodedArtist = URLEncoder.encode(artist, "UTF-8"); encodedSong = URLEncoder.encode(track, "UTF-8"); String baseUrl = "http://ws.audioscrobbler.com/2.0/?method=track.getcorrection&artist=%s&track=%s&api_key=%s&format=json"; JSONObject json = new JSONObject( getUrlAsString(new URL(String.format(baseUrl, encodedArtist, encodedSong, Keys.LastFM)))) .getJSONObject("corrections").getJSONObject("correction"); artist = json.getJSONObject("track").getJSONObject("artist").getString("name"); track = json.getJSONObject("track").getString("name"); return new String[] { artist, track }; } catch (JSONException | IOException e) { e.printStackTrace(); } return new String[] { null, null }; }
From source file:Bing.java
/** * * @param query - query to use for the search * @return - a json array of results. each contains a title, description, url, * and some other metadata that can be easily extracted since its in json format *//* w ww . java2s. co m*/ public static JSONArray search(String query) { try { query = "'" + query + "'"; String encodedQuery = URLEncoder.encode(query, "UTF-8"); System.out.println(encodedQuery); // URL url = new URL("https://api.datamarket.azure.com/Bing/Search/v1/Composite?Sources=%27web%27&Query=%27car%27"); // URL url = new URL("http://csb.stanford.edu/class/public/pages/sykes_webdesign/05_simple.html"); String webPage = "https://api.datamarket.azure.com/Bing/Search/v1/Composite?Sources=%27web%27&Query=" + encodedQuery + "&$format=JSON"; String name = "6604d12c-3e89-4859-8013-3132f78c1595"; String password = "cefgNRl3OL4PrJJvssxkqLw0VKfYNCgyTe8wNXotUmQ"; String authString = name + ":" + password; System.out.println("auth string: " + authString); byte[] authEncBytes = Base64.encodeBase64(authString.getBytes()); String authStringEnc = new String(authEncBytes); System.out.println("Base64 encoded auth string: " + authStringEnc); URL url = new URL(webPage); URLConnection urlConnection = url.openConnection(); urlConnection.setRequestProperty("Authorization", "Basic " + authStringEnc); BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); String inputLine; StringBuilder response = new StringBuilder(); while ((inputLine = in.readLine()) != null) { response.append(inputLine).append("\n"); } in.close(); JSONParser parser = new JSONParser(); JSONArray arr = (JSONArray) ((JSONObject) ((JSONObject) parser.parse(response.toString())).get("d")) .get("results"); JSONObject obj = (JSONObject) arr.get(0); JSONArray out = (JSONArray) obj.get("Web"); return out; // } catch (MalformedURLException ex) { Logger.getLogger(Bing.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(Bing.class.getName()).log(Level.SEVERE, null, ex); } catch (ParseException ex) { Logger.getLogger(Bing.class.getName()).log(Level.SEVERE, null, ex); } return null; }
From source file:com.handlerexploit.news.data.YQLHelper.java
public static String query(String query) { String fullUrl = null;/*from w w w .jav a 2 s. c o m*/ try { fullUrl = "http://query.yahooapis.com/v1/public/yql?format=json&q=" + URLEncoder.encode(query, "US-ASCII"); } catch (UnsupportedEncodingException e) { Log.e(TAG, "Error encoding URL", e); } if (fullUrl != null) { String queryResponse = null; try { String userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2"; AndroidHttpClient androidHttpClient = AndroidHttpClient.newInstance(userAgent); // TODO Look into using something else for pre-froyo queryResponse = EntityUtils.toString(androidHttpClient.execute(new HttpGet(fullUrl)).getEntity()); androidHttpClient.close(); JSONObject jsonQuery = new JSONObject(queryResponse).getJSONObject("query"); if (jsonQuery.getInt("count") > 0) { return jsonQuery.getJSONObject("results").toString(); } else { Log.d(TAG, "YQL returned empty - " + fullUrl + " - " + jsonQuery.toString()); } } catch (Throwable e) { Log.e(TAG, "An error occured while parsing yql query - " + queryResponse, e); } } return null; }
From source file:com.geecko.QuickLyric.utils.LastFMCorrection.java
public static String[] getCorrection(String artist, String track) { if ((artist == null) || (track == null)) return new String[] { null, null }; String encodedArtist;// ww w .j av a2s . co m String encodedSong; try { encodedArtist = URLEncoder.encode(artist, "UTF-8"); encodedSong = URLEncoder.encode(track, "UTF-8"); String baseUrl = "http://ws.audioscrobbler.com/2.0/?method=track.getcorrection&artist=%s&track=%s&api_key=%s&format=json"; JSONObject json = new JSONObject( Net.getUrlAsString(new URL(String.format(baseUrl, encodedArtist, encodedSong, Keys.lastFM)))) .getJSONObject("corrections").getJSONObject("correction"); artist = json.getJSONObject("track").getJSONObject("artist").getString("name"); track = json.getJSONObject("track").getString("name"); return new String[] { artist, track }; } catch (JSONException | IOException ignored) { } return new String[] { null, null }; }
From source file:Main.java
public static String appendParameter(String url, String name, String value) { if (name == null || value == null) { return url; }/*ww w .j a v a 2 s . c om*/ value = value.trim(); if (value.length() == 0) { return url; } try { value = URLEncoder.encode(value, "utf-8"); } catch (UnsupportedEncodingException e) { } int index = url.indexOf('?', url.lastIndexOf('/') + 1); char delimiter = (index == -1) ? '?' : '&'; while (index != -1) { final int start = index + 1; final int eqIndex = url.indexOf('=', start); index = url.indexOf('&', start); if (eqIndex != -1 && url.substring(start, eqIndex).equals(name)) { final int end = (index != -1 ? index : url.length()); if (url.substring(eqIndex + 1, end).equals(value)) { return url; } else { return new StringBuilder(url).replace(eqIndex + 1, end, value).toString(); } } } return new StringBuilder(url).append(delimiter).append(name).append('=').append(value).toString(); }
From source file:com.surfs.storage.common.util.HttpUtils.java
public static String getUrl(String ip, String port, String servicePath, String serviceName, String... args) throws UnsupportedEncodingException { StringBuilder params = new StringBuilder(); params.append("http://"); params.append(ip);//from ww w. j a va 2 s . c om params.append(":"); params.append(port); params.append(servicePath); params.append(serviceName); // args for (String arg : args) { params.append("/"); params.append(URLEncoder.encode(arg, "UTF-8")); } return params.toString(); }
From source file:com.jivesoftware.sdk.util.JiveSDKUtils.java
public static String encodeUrl(String url) { try {/*w w w.j a v a 2s . c o m*/ return URLEncoder.encode(url, StandardCharsets.UTF_8.name()); } catch (UnsupportedEncodingException uee) { log.warn("Failed encoding URL using UTF-8 charset" + uee.getMessage()); //noinspection deprecation return url; } // end try/catch }