List of usage examples for java.net URL openConnection
public URLConnection openConnection() throws java.io.IOException
From source file:com.breadwallet.tools.threads.ImportPrivKeyTask.java
private static String callURL(String myURL) { // System.out.println("Requested URL_EA:" + myURL); StringBuilder sb = new StringBuilder(); URLConnection urlConn = null; InputStreamReader in = null;/*from w ww. ja v a 2 s. co m*/ try { URL url = new URL(myURL); urlConn = url.openConnection(); if (urlConn != null) urlConn.setReadTimeout(60 * 1000); if (urlConn != null && urlConn.getInputStream() != null) { in = new InputStreamReader(urlConn.getInputStream(), Charset.defaultCharset()); BufferedReader bufferedReader = new BufferedReader(in); int cp; while ((cp = bufferedReader.read()) != -1) { sb.append((char) cp); } bufferedReader.close(); } assert in != null; in.close(); } catch (Exception e) { return null; } return sb.toString(); }
From source file:com.beginner.core.utils.SmsUtil.java
public static String SMS(String postData, String postUrl) { try {/* w w w . jav a 2 s . c o m*/ //??POST URL url = new URL(postUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setUseCaches(false); conn.setDoOutput(true); conn.setRequestProperty("Content-Length", "" + postData.length()); OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8"); out.write(postData); out.flush(); out.close(); //??? if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) { System.out.println("connect failed!"); return ""; } //?? String line, result = ""; BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8")); while ((line = in.readLine()) != null) { result += line + "\n"; } in.close(); return result; } catch (IOException e) { e.printStackTrace(System.out); } return ""; }
From source file:net.daporkchop.porkbot.util.HTTPUtils.java
private static HttpURLConnection createUrlConnection(@NonNull URL url) throws IOException { final HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(15000); connection.setReadTimeout(15000);/*from www. j av a2 s . com*/ connection.setUseCaches(false); return connection; }
From source file:com.android.dialer.omni.PlaceUtil.java
/** * Executes a post request and return a JSON object * @param url The API URL//from w w w . j av a2s.com * @param data The data to post in POST field * @return the JSON object * @throws IOException * @throws JSONException */ public static JSONObject postJsonRequest(String url, String postData) throws IOException, JSONException { URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); con.setRequestMethod("POST"); if (DEBUG) Log.d(TAG, "Posting: " + postData + " to " + url); // Send post request con.setDoOutput(true); DataOutputStream wr = new DataOutputStream(con.getOutputStream()); wr.writeBytes(postData); wr.flush(); wr.close(); int responseCode = con.getResponseCode(); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); JSONObject json = new JSONObject(response.toString()); return json; }
From source file:com.mopaas_mobile.http.BaseHttpRequester.java
public static String doGET(String urlstr, List<BasicNameValuePair> params) throws IOException { String result = null;/*from w ww . java2 s . c om*/ String content = ""; for (int i = 0; i < params.size(); i++) { content = content + "&" + URLEncoder.encode(((NameValuePair) params.get(i)).getName(), "UTF-8") + "=" + URLEncoder.encode(((NameValuePair) params.get(i)).getValue(), "UTF-8"); } URL url = new URL(urlstr + "?" + content.substring(1)); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true); connection.setRequestMethod("GET"); connection.setUseCaches(false); connection.connect(); InputStream is = connection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8")); StringBuffer b = new StringBuffer(); int ch; while ((ch = br.read()) != -1) { b.append((char) ch); } result = b.toString().trim(); connection.disconnect(); return result; }
From source file:de.akquinet.android.androlog.reporter.PostReporter.java
/** * Executes the given request as a HTTP POST action. * * @param url/* ww w . j a v a2 s .c o m*/ * the url * @param params * the parameter * @return the response as a String. * @throws IOException * if the server cannot be reached */ public static void post(URL url, String params) throws IOException { URLConnection conn = url.openConnection(); if (conn instanceof HttpURLConnection) { ((HttpURLConnection) conn).setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/json"); } OutputStreamWriter writer = null; try { conn.setDoOutput(true); writer = new OutputStreamWriter(conn.getOutputStream(), Charset.forName("UTF-8")); // write parameters writer.write(params); writer.flush(); } finally { if (writer != null) { writer.close(); } } }
From source file:com.consumerkarma.TTS.SpeechAuth.java
/** * Sets up an OAuth client credentials authentication. * Follow this with a call to fetchTo() to actually load the data. * @param oauthService the URL of the OAuth client credentials service * @param apiKey the OAuth client ID//www .j av a2s .c om * @param apiSecret the OAuth client secret * @throws IllegalArgumentException for bad URL, etc. **/ public static SpeechAuth forService(String oauthService, String apiKey, String apiSecret) throws IllegalArgumentException { try { URL url = new URL(oauthService); HttpURLConnection request = (HttpURLConnection) url.openConnection(); String data = String.format(Locale.US, OAUTH_DATA, apiKey, apiSecret); byte[] bytes = data.getBytes("UTF8"); request.setConnectTimeout(CONNECT_TIMEOUT); request.setReadTimeout(READ_TIMEOUT); return new SpeechAuth(request, bytes); } catch (IOException e) { throw new IllegalArgumentException(e); } catch (ClassCastException e) { throw new IllegalArgumentException("URL must be HTTP: " + oauthService, e); } }
From source file:com.ibm.jaql.lang.expr.hadoop.Util.java
/** * Modified from Hadoop's JobClient/*w w w . j a va 2s . c o m*/ * * @param taskId * @param taskLogUrl */ private static void logTaskLog(TaskAttemptID taskId, URL taskLogUrl) { try { URLConnection connection = taskLogUrl.openConnection(); BufferedReader input = new BufferedReader(new InputStreamReader(connection.getInputStream())); try { String logData = null; while ((logData = input.readLine()) != null) { if (logData.length() > 0) { LOG.info("[" + taskId + "]" + logData); } } } finally { input.close(); } } catch (IOException ioe) { LOG.warn("Error reading task output" + ioe.getMessage()); } }
From source file:com.surfs.storage.common.util.HttpUtils.java
public static String invokeHttpForGet(String path, String... agrs) throws IOException { URL url = new URL(path); LogFactory.info("rest url:" + url.toString()); HttpURLConnection con = null; try {/*from w ww . j av a 2 s.com*/ con = (HttpURLConnection) url.openConnection(); con.setRequestMethod("GET"); con.setConnectTimeout(10000); con.setReadTimeout(1000 * 60 * 30); con.setDoOutput(true); con.setDoInput(true); con.setUseCaches(false); for (String string : agrs) { con.setRequestProperty("Content-type", "application/json"); con.setRequestMethod("POST"); OutputStream out = con.getOutputStream(); out.write(string.getBytes("UTF-8")); } if (con.getResponseCode() != 200) throw new ConnectException(con.getResponseMessage()); InputStream is = con.getInputStream(); return readResponse(is); } catch (IOException e) { throw e; } finally { if (con != null) { con.disconnect(); } } }
From source file:mp3downloader.ZingSearch.java
private static String getSearchResult(String term, int searchType) throws UnsupportedEncodingException, MalformedURLException, IOException { String data = "{\"kw\": \"" + term + "\", \"t\": " + searchType + ", \"rc\": 50}"; String jsonData = URLEncoder.encode(Base64.getEncoder().encodeToString(data.getBytes("UTF-8")), "UTF-8"); String signature = hash_hmac(jsonData, privateKey); String urlString = searchUrl + "publicKey=" + publicKey + "&signature=" + signature + "&jsondata=" + jsonData;/*from w w w . ja v a2s . c o m*/ URL url = new URL(urlString); URLConnection urlConn = url.openConnection(); urlConn.addRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36"); InputStream is = urlConn.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8"))); String line; StringBuilder sb = new StringBuilder(); while ((line = reader.readLine()) != null) { sb.append(line); sb.append("\n"); } is.close(); String content = sb.toString(); return content; }