public static void main(String[] args) throws Exception { String google = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q="; String search = "stackoverflow"; String charset = "UTF-8"; URL url = new URL(google + URLEncoder.encode(search, charset)); Reader reader = new InputStreamReader(url.openStream(), charset); GoogleResults results = new Gson().fromJson(reader, GoogleResults.class); // Show title and URL of 1st result. System.out.println(results.getResponseData().getResults().get(0).getTitle()); System.out.println(results.getResponseData().getResults().get(0).getUrl()); }
private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException{ StringBuilder result = new StringBuilder(); boolean first = true; for(Map.Entry<String, String> entry : params.entrySet()){ if (first) first = false; else result.append("&"); result.append(URLEncoder.encode(entry.getKey(), "UTF-8")); result.append("="); result.append(URLEncoder.encode(entry.getValue(), "UTF-8")); } return result.toString(); }
URL url = new URL("http://example.net/new-message.php"); Map<String,Object> params = new LinkedHashMap<>(); params.put("name", "Freddie the Fish"); for (Map.Entry<String,Object> param : params.entrySet()) { if (postData.length() != 0) postData.append('&'); postData.append(URLEncoder.encode(param.getKey(), "UTF-8")); postData.append('='); postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8")); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); conn.getOutputStream().write(postDataBytes); Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
String rawData = "id=10"; String type = "application/x-www-form-urlencoded"; String encodedData = URLEncoder.encode( rawData ); URL u = new URL("http://www.example.com/page.php"); HttpURLConnection conn = (HttpURLConnection) u.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("POST"); conn.setRequestProperty( "Content-Type", type ); conn.setRequestProperty( "Content-Length", String.valueOf(encodedData.length())); OutputStream os = conn.getOutputStream(); os.write(encodedData.getBytes());
protected InputStream postMessage(Map<String, String>content, URL destination, String encodeCharset) throws IOException { HttpURLConnection connection = (HttpURLConnection) destination.openConnection(); connection.setDoInput(true); connection.setDoOutput(true); osw.write(URLEncoder.encode(key, encodeCharset)); osw.write("="); osw.write(URLEncoder.encode(value, encodeCharset));
void ping() throws IOException { pingCount++; Map<String, String> usageDataMap = collector.getUdcParams(); StringBuilder uri = new StringBuilder( "http://" + address + "/" + "?" ); for ( Map.Entry<String,String> entry : usageDataMap.entrySet() ) { uri.append( entry.getKey() ); uri.append( "=" ); uri.append( URLEncoder.encode( entry.getValue(), StandardCharsets.UTF_8.name() ) ); uri.append( "+" ); } uri.append( PING + "=" ).append( pingCount ); URL url = new URL( uri.toString() ); URLConnection con = url.openConnection(); con.setDoInput( true ); con.setDoOutput( false ); con.setUseCaches( false ); con.connect(); con.getInputStream().close(); }
/** * {@inheritDoc} */ public URL run() { try { return new URL(URL_SCHEMA, URLEncoder.encode(typeName.replace('.', '/'), ENCODING), NO_PORT, NO_FILE, new ByteArrayUrlStreamHandler(binaryRepresentation)); } catch (MalformedURLException exception) { throw new IllegalStateException("Cannot create URL for " + typeName, exception); } catch (UnsupportedEncodingException exception) { throw new IllegalStateException("Could not find encoding: " + ENCODING, exception); } }
String message = URLEncoder.encode("my message", "UTF-8"); URL url = new URL("http://www.example.com/comment"); .openConnection();
public static void main(String[] args) throws Exception { String google = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q="; String search = "stackoverflow"; String charset = "UTF-8"; URL url = new URL(google + URLEncoder.encode(search, charset)); Reader reader = new InputStreamReader(url.openStream(), charset); GoogleResults results = new Gson().fromJson(reader, GoogleResults.class); // Show title and URL of 1st result. System.out.println(results.getResponseData().getResults().get(0).getTitle()); System.out.println(results.getResponseData().getResults().get(0).getUrl()); }
Map<String,String> arguments = new HashMap<>(); arguments.put("username", "root"); arguments.put("password", "sjh76HSn!"); // This is a fake password obviously StringJoiner sj = new StringJoiner("&"); for(Map.Entry<String,String> entry : arguments.entrySet()) sj.add(URLEncoder.encode(entry.getKey(), "UTF-8") + "=" + URLEncoder.encode(entry.getValue(), "UTF-8")); byte[] out = sj.toString().getBytes(StandardCharsets.UTF_8); int length = out.length;
public int perfQuery(String query) throws Exception { LOGGER.debug("Running perf query on Pinot Cluster"); String encodedQuery = URLEncoder.encode(query, "UTF-8"); String brokerUrl = _perfUrl + encodedQuery; LOGGER.info("Executing command: " + brokerUrl); URLConnection conn = new URL(brokerUrl).openConnection(); conn.setDoOutput(true); long startTime = System.currentTimeMillis(); InputStream input = conn.getInputStream(); long endTime = System.currentTimeMillis(); BufferedReader reader = new BufferedReader(new InputStreamReader(input, "UTF-8")); StringBuilder sb = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { sb.append(line); } LOGGER.debug("Actual response: " + sb.toString()); return (int) (endTime - startTime); } }
@Restricted(NoExternalUse.class) public @Nonnull FormValidation updateDirectlyNow(boolean signatureCheck) throws IOException { return updateData(DownloadService.loadJSON(new URL(getUrl() + "?id=" + URLEncoder.encode(getId(), "UTF-8") + "&version=" + URLEncoder.encode(Jenkins.VERSION, "UTF-8"))), signatureCheck); }
URL u = new URL(args[ 0 ]); URLConnection c = u.openConnection(); if ( ! ( c instanceof HttpURLConnection ) ) { throw new IllegalArgumentException("Not a HTTP url"); oos.close(); byte[] data = bos.toByteArray(); String requestBody = "javax.faces.ViewState=" + URLEncoder.encode(Base64.encodeBase64String(data), "US-ASCII"); os.write(requestBody.getBytes("US-ASCII")); os.close();
private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException{ StringBuilder result = new StringBuilder(); boolean first = true; for(Map.Entry<String, String> entry : params.entrySet()){ if (first) first = false; else result.append("&"); result.append(URLEncoder.encode(entry.getKey(), "UTF-8")); result.append("="); result.append(URLEncoder.encode(entry.getValue(), "UTF-8")); } return result.toString(); }
boolean firstPrm =!urlText.contains("?"); for (String key : additionalMapData.keySet()) { urlText += (firstPrm ? "?" : "&") + key + "=" + URLEncoder.encode(additionalMapData.get(key), "UTF-8"); firstPrm = false; url = new URL(urlText); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoInput(true); conn.setDoOutput(true); StringBuilder responseBody = new StringBuilder(); if (is != null) { BufferedReader in = new BufferedReader(new InputStreamReader(is, "UTF-8")); //$NON-NLS-1$ String s; boolean first = true; while ((s = in.readLine()) != null) { if(first){ first = false;
private static List<URL> getDirectoryClassPath(String aDir) { try { final List<URL> list = new LinkedList<URL>(); final File dir = new File(aDir); final URL directoryURL = dir.toURI().toURL(); final String[] children = dir.list(); if( children != null ) { for( final String element : children ) { if( element.endsWith(".jar") ) { final URL url = new URL(directoryURL, URLEncoder.encode(element, "UTF-8")); list.add(url); } } } return list; } catch( Exception e ) { throw Log.errRTExcept(e); } }
@Test public void testUnderscoresInJSONPCallback() throws Exception { String callback = "my_function"; URL url = new URL( baseUrl, "/jmx?qry=java.lang:type=Memory&callback="+URLEncoder.encode(callback, "UTF-8")); HttpURLConnection cnxn = (HttpURLConnection) url.openConnection(); assertEquals(HttpServletResponse.SC_OK, cnxn.getResponseCode()); } }
private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException{ StringBuilder result = new StringBuilder(); boolean first = true; for(Map.Entry<String, String> entry : params.entrySet()){ if (first) first = false; else result.append("&"); result.append(URLEncoder.encode(entry.getKey(), "UTF-8")); result.append("="); result.append(URLEncoder.encode(entry.getValue(), "UTF-8")); } return result.toString(); }
// Construct data String data = URLEncoder.encode("key1", "UTF-8") + "=" + URLEncoder.encode("value1", "UTF-8"); data += "&" + URLEncoder.encode("key2", "UTF-8") + "=" + URLEncoder.encode("value2", "UTF-8"); // Send data URL url = new URL("http://hostname:80/cgi"); URLConnection conn = url.openConnection(); conn.setDoOutput(true); OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); wr.write(data); wr.flush(); // Get the response BufferedReader rd = new BufferedReader( new InputStreamReader(conn.getInputStream())); String line; while ((line = rd.readLine()) != null) { // Process line... } wr.close(); rd.close();