List of usage examples for java.net URLConnection setReadTimeout
public void setReadTimeout(int timeout)
From source file:com.bt.heliniumstudentapp.UpdateClass.java
@Override protected String doInBackground(Void... Void) { try {//www .jav a 2 s .com URLConnection connection = new URL(HeliniumStudentApp.URL_UPDATE_CHANGELOG).openConnection(); connection.setConnectTimeout(HeliniumStudentApp.TIMEOUT_CONNECT); connection.setReadTimeout(HeliniumStudentApp.TIMEOUT_READ); connection.setRequestProperty("Accept-Charset", HeliniumStudentApp.CHARSET); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + HeliniumStudentApp.CHARSET); connection.connect(); final Scanner line = new Scanner(connection.getInputStream()).useDelimiter("\\A"); final String html = line.hasNext() ? line.next() : ""; ((HttpURLConnection) connection).disconnect(); if (((HttpURLConnection) connection).getResponseCode() == 200) return html; else return null; } catch (IOException e) { return null; } }
From source file:org.apache.marmotta.ucuenca.wk.commons.function.SemanticDistance.java
private synchronized String http(String s) throws SQLException, IOException { Statement stmt = conn.createStatement(); String sql;/*from www . j ava2 s . com*/ sql = "SELECT * FROM cache where cache.key='" + commonservices.getMD5(s) + "'"; java.sql.ResultSet rs = stmt.executeQuery(sql); String resp = ""; if (rs.next()) { resp = rs.getString("value"); rs.close(); stmt.close(); } else { rs.close(); stmt.close(); final URL url = new URL(s); final URLConnection connection = url.openConnection(); connection.setConnectTimeout(60000); connection.setReadTimeout(60000); connection.addRequestProperty("User-Agent", "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:44.0) Gecko/20100101 Firefox/44.0"); connection.addRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); final Scanner reader = new Scanner(connection.getInputStream(), "UTF-8"); while (reader.hasNextLine()) { final String line = reader.nextLine(); resp += line + "\n"; } reader.close(); try { JsonParser parser = new JsonParser(); parser.parse(resp); PreparedStatement stmt2 = conn.prepareStatement("INSERT INTO cache (key, value) values (?, ?)"); stmt2.setString(1, commonservices.getMD5(s)); stmt2.setString(2, resp); stmt2.executeUpdate(); stmt2.close(); } catch (Exception e) { } } return resp; }
From source file:shapeways.api.robocreator.RoboCreatorWeb.java
/** * Get the public facing hostname for this machine. Uses AWS metadata service. *///from w w w . j a va 2 s .c o m protected String getInstanceMetadata(String name, String defValue) { ByteArrayOutputStream baos = null; BufferedOutputStream bout = null; String ret_val = null; InputStream is = null; try { URL url = new URL("http://169.254.169.254/latest/meta-data/" + name); URLConnection urlConn = url.openConnection(); urlConn.setConnectTimeout(5000); urlConn.setReadTimeout(15000); urlConn.setAllowUserInteraction(false); urlConn.setDoOutput(true); is = new BufferedInputStream(urlConn.getInputStream()); baos = new ByteArrayOutputStream(); bout = new BufferedOutputStream(baos, 1024); int buffSize = 8 * 1024; byte data[] = new byte[buffSize]; int count; while ((count = is.read(data, 0, buffSize)) >= 0) { baos.write(data, 0, count); } ret_val = baos.toString(); } catch (Exception e) { // ignore //e.printStackTrace(); } finally { try { bout.close(); is.close(); } catch (Exception e) { // ignore } } if (ret_val == null) { ret_val = defValue; } return ret_val; }
From source file:com.comcast.cdn.traffic_control.traffic_router.core.util.Fetcher.java
protected HttpURLConnection getConnection(final String url, final String data, final String requestMethod, final long lastFetchTime) throws IOException { String method = GET_STR;// w w w . j a v a2 s . c o m if (requestMethod != null) { method = requestMethod; } LOGGER.info(method + "ing: " + url + "; timeout is " + timeout); final URLConnection connection = new URL(url).openConnection(); connection.setIfModifiedSince(lastFetchTime); if (timeout != 0) { connection.setConnectTimeout(timeout); connection.setReadTimeout(timeout); } final HttpURLConnection http = (HttpURLConnection) connection; if (connection instanceof HttpsURLConnection) { final HttpsURLConnection https = (HttpsURLConnection) connection; https.setHostnameVerifier(new HostnameVerifier() { @Override public boolean verify(final String arg0, final SSLSession arg1) { return true; } }); } http.setInstanceFollowRedirects(false); http.setRequestMethod(method); http.setAllowUserInteraction(true); for (final String key : requestProps.keySet()) { http.addRequestProperty(key, requestProps.get(key)); } if (method.equals(POST_STR) && data != null) { http.setDoOutput(true); // Triggers POST. try (final OutputStream output = http.getOutputStream()) { output.write(data.getBytes(UTF8_STR)); } } connection.connect(); return http; }
From source file:com.util.httpHistorial.java
public List<Llamadas> getHistorial(String idAccount, String page, String max, String startDate, String endDate, String destination) {//w ww . j a v a 2 s.co m // String idAccount = "2"; // String page = "1"; // String max = "10"; //String startDate = "2016-09-20 00:00:00"; // String endDate = "2016-10-30 23:59:59"; // String destination = ""; System.out.println("OBTENER SOLO UN ARRAY DE CADENA JSON"); String myURL = "http://192.168.5.44/app_dev.php/cus/cdrs/history/" + idAccount + ".json"; System.out.println("Requested URL:" + myURL); StringBuilder sb = new StringBuilder(); URLConnection urlConn = null; InputStreamReader in = null; try { URL url = new URL(myURL); urlConn = url.openConnection(); if (urlConn != null) { urlConn.setReadTimeout(60 * 1000); urlConn.setDoOutput(true); String data = URLEncoder.encode("page", "UTF-8") + "=" + URLEncoder.encode(page, "UTF-8"); data += "&" + URLEncoder.encode("max", "UTF-8") + "=" + URLEncoder.encode(max, "UTF-8"); data += "&" + URLEncoder.encode("startDate", "UTF-8") + "=" + URLEncoder.encode(startDate, "UTF-8"); data += "&" + URLEncoder.encode("endDate", "UTF-8") + "=" + URLEncoder.encode(endDate, "UTF-8"); data += "&" + URLEncoder.encode("destination", "UTF-8") + "=" + URLEncoder.encode(destination, "UTF-8"); System.out.println("los Datos a enviar por POST son " + data); try ( //obtenemos el flujo de escritura OutputStreamWriter wr = new OutputStreamWriter(urlConn.getOutputStream())) { //escribimos wr.write(data); wr.flush(); //cerramos la conexin } } if (urlConn != null && urlConn.getInputStream() != null) { in = new InputStreamReader(urlConn.getInputStream(), Charset.defaultCharset()); BufferedReader bufferedReader = new BufferedReader(in); if (bufferedReader != null) { int cp; while ((cp = bufferedReader.read()) != -1) { sb.append((char) cp); } bufferedReader.close(); } } in.close(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException("Exception while calling URL:" + myURL, e); } String jsonResult = sb.toString(); System.out.println("DATOS ENVIADOS DEL SERVIDOR " + sb.toString()); System.out.println( "\n\n--------------------OBTENEMOS OBJETO JSON NATIVO DE LA PAGINA, USAMOS EL ARRAY DATA---------------------------\n\n"); JSONObject objJason = new JSONObject(jsonResult); // JSONArray dataJson = new JSONArray(); // dataJson = objJason.getJSONArray("data"); String jdata = objJason.optString("data"); String mensaje = objJason.optString("message"); System.out.println("\n\n MENSAJE DEL SERVIDOR " + mensaje); //System.out.println(" el objeto jdata es "+jdata); objJason = new JSONObject(jdata); // System.out.println("objeto normal 1 " + objJason.toString()); // jdata = objJason.optString("items"); // System.out.println("\n\n el objeto jdata es " + jdata); JSONArray jsonArray = new JSONArray(); Gson gson = new Gson(); //objJason = gson.t jsonArray = objJason.getJSONArray("items"); // System.out.println("\n\nEL ARRAY FINAL ES " + jsonArray.toString()); List<Llamadas> llamadas = new ArrayList<Llamadas>(); for (int i = 0; i < jsonArray.length(); i++) { Llamadas llamada = new Llamadas(); llamada.setNo(i + 1); llamada.setInicioLLamada(jsonArray.getJSONObject(i).getString("callstart")); llamada.setNumero(jsonArray.getJSONObject(i).getString("callednum")); llamada.setPais_operador(jsonArray.getJSONObject(i).getString("notes")); llamada.setDuracionSegundos(String.valueOf(jsonArray.getJSONObject(i).getBigDecimal("billseconds"))); llamada.setCostoTotal(String.valueOf(jsonArray.getJSONObject(i).getBigDecimal("cost"))); llamada.setCostoMinuto(String.valueOf(jsonArray.getJSONObject(i).getBigDecimal("rate_cost"))); long minutos = Long.parseLong(llamada.getDuracionSegundos()) / 60; llamada.setDuracionMinutos(minutos); llamadas.add(llamada); } for (int i = 0; i < llamadas.size(); i++) { System.out.print("\n\nNo" + llamadas.get(i).getNo()); System.out.print(" Fecna " + llamadas.get(i).getInicioLLamada()); System.out.print(" Numero " + llamadas.get(i).getNumero()); System.out.print(" Pais-Operador " + llamadas.get(i).getPais_operador()); System.out.print(" Cantidad de segundos " + llamadas.get(i).getDuracionSegundos()); System.out.print(" Costo total " + llamadas.get(i).getCostoTotal()); System.out.print(" costo por minuto " + llamadas.get(i).getCostoMinuto()); System.out.print(" costo por minuto " + llamadas.get(i).getDuracionMinutos()); } /** * List<String> list = new ArrayList<String>(); for (int i = 0; i < * jsonArray.length(); i++) { list.add(String.valueOf(i)); * list.add(jsonArray.getJSONObject(i).getString("callstart")); * list.add(jsonArray.getJSONObject(i).getString("callednum")); * list.add(jsonArray.getJSONObject(i).getString("notes")); * list.add(String.valueOf(jsonArray.getJSONObject(i).getBigDecimal("cost"))); * list.add(String.valueOf(jsonArray.getJSONObject(i).getBigDecimal("billseconds"))); * list.add(String.valueOf(jsonArray.getJSONObject(i).getBigDecimal("rate_cost"))); * } System.out.println("\n\nel array java contiene " + * list.toString()); * */ return llamadas; }
From source file:org.adaway.service.UpdateService.java
/** * Check for updates of hosts sources/* www .java 2 s . c o m*/ * * @return return code */ private int checkForUpdates() { Cursor enabledHostsSourcesCursor; long currentLastModifiedLocal; long currentLastModifiedOnline; boolean updateAvailable = false; int returnCode = StatusCodes.ENABLED; // default return code if (Utils.isAndroidOnline(mService)) { mNumberOfFailedDownloads = 0; mNumberOfDownloads = 0; // get cursor over all enabled hosts source enabledHostsSourcesCursor = ProviderHelper.getEnabledHostsSourcesCursor(mService); // iterate over all hosts sources in db with cursor if (enabledHostsSourcesCursor != null && enabledHostsSourcesCursor.moveToFirst()) { do { mNumberOfDownloads++; // get url and lastModified from db String currentUrl = enabledHostsSourcesCursor .getString(enabledHostsSourcesCursor.getColumnIndex("url")); currentLastModifiedLocal = enabledHostsSourcesCursor .getLong(enabledHostsSourcesCursor.getColumnIndex("last_modified_local")); try { Log.v(Constants.TAG, "Checking hosts file: " + currentUrl); /* build connection */ URL mURL = new URL(currentUrl); URLConnection connection = mURL.openConnection(); connection.setConnectTimeout(15000); connection.setReadTimeout(30000); currentLastModifiedOnline = connection.getLastModified(); Log.d(Constants.TAG, "mConnectionLastModified: " + currentLastModifiedOnline + " (" + DateUtils.longToDateString(mService, currentLastModifiedOnline) + ")"); Log.d(Constants.TAG, "mCurrentLastModified: " + currentLastModifiedLocal + " (" + DateUtils.longToDateString(mService, currentLastModifiedLocal) + ")"); // check if file is available connection.connect(); connection.getInputStream(); // check if update available for this hosts file if (currentLastModifiedOnline > currentLastModifiedLocal) { updateAvailable = true; } // save last modified online for later viewing in list ProviderHelper.updateHostsSourceLastModifiedOnline(mService, enabledHostsSourcesCursor .getInt(enabledHostsSourcesCursor.getColumnIndex(HostsSources._ID)), currentLastModifiedOnline); } catch (Exception e) { Log.e(Constants.TAG, "Exception while downloading from " + currentUrl, e); mNumberOfFailedDownloads++; // set last_modified_online of failed download to 0 (not available) ProviderHelper.updateHostsSourceLastModifiedOnline(mService, enabledHostsSourcesCursor .getInt(enabledHostsSourcesCursor.getColumnIndex(HostsSources._ID)), 0); } } while (enabledHostsSourcesCursor.moveToNext()); } // close cursor in the end if (enabledHostsSourcesCursor != null && !enabledHostsSourcesCursor.isClosed()) { enabledHostsSourcesCursor.close(); } // if all downloads failed return download_fail error if (mNumberOfDownloads == mNumberOfFailedDownloads && mNumberOfDownloads != 0) { returnCode = StatusCodes.DOWNLOAD_FAIL; } } else { // only report no connection when not in background if (!mBackgroundExecution) { returnCode = StatusCodes.NO_CONNECTION; } else { Log.e(Constants.TAG, "Should not happen! In background execution is no connection available!"); } } // set return code if update is available if (updateAvailable) { returnCode = StatusCodes.UPDATE_AVAILABLE; } // check if hosts file is applied if (!ApplyUtils.isHostsFileCorrect(mService, Constants.ANDROID_SYSTEM_ETC_HOSTS)) { returnCode = StatusCodes.DISABLED; } return returnCode; }
From source file:org.geoserver.wps.executor.RemoteRequestInputProvider.java
@Override protected Object getValueInternal(ProgressListener listener) throws Exception { InputReferenceType ref = input.getReference(); URL destination = new URL(ref.getHref()); HttpMethod method = null;// w w w . j a va 2 s .c o m GetMethod refMethod = null; InputStream input = null; InputStream refInput = null; // execute the request listener.started(); try { if ("file".equalsIgnoreCase(destination.getProtocol())) { File file = DataUtilities.urlToFile(destination); if (maxSize > 0 && maxSize < file.length()) { throw new WPSException("Input " + getInputId() + " size " + file.length() + " exceeds maximum allowed size of " + maxSize, "NoApplicableCode", getInputId()); } input = new FileInputStream(file); } else if ("http".equalsIgnoreCase(destination.getProtocol())) { // setup the client HttpClient client = new HttpClient(); // setting timeouts (30 seconds, TODO: make this configurable) HttpConnectionManagerParams params = new HttpConnectionManagerParams(); params.setSoTimeout(timeout); params.setConnectionTimeout(timeout); // TODO: make the http client a well behaved http client, no more than x connections // per server (x admin configurable maybe), persistent connections and so on HttpConnectionManager manager = new SimpleHttpConnectionManager(); manager.setParams(params); client.setHttpConnectionManager(manager); // prepare either a GET or a POST request if (ref.getMethod() == null || ref.getMethod() == MethodType.GET_LITERAL) { GetMethod get = new GetMethod(ref.getHref()); get.setFollowRedirects(true); method = get; } else { String encoding = ref.getEncoding(); if (encoding == null) { encoding = "UTF-8"; } PostMethod post = new PostMethod(ref.getHref()); Object body = ref.getBody(); if (body == null) { if (ref.getBodyReference() != null) { URL refDestination = new URL(ref.getBodyReference().getHref()); if ("http".equalsIgnoreCase(refDestination.getProtocol())) { // open with commons http client refMethod = new GetMethod(ref.getBodyReference().getHref()); refMethod.setFollowRedirects(true); client.executeMethod(refMethod); refInput = refMethod.getResponseBodyAsStream(); } else { // open with the built-in url management URLConnection conn = refDestination.openConnection(); conn.setConnectTimeout(timeout); conn.setReadTimeout(timeout); refInput = conn.getInputStream(); } post.setRequestEntity( new InputStreamRequestEntity(refInput, complexPPIO.getMimeType())); } else { throw new WPSException("A POST request should contain a non empty body"); } } else if (body instanceof String) { post.setRequestEntity( new StringRequestEntity((String) body, complexPPIO.getMimeType(), encoding)); } else { throw new WPSException("The request body should be contained in a CDATA section, " + "otherwise it will get parsed as XML instead of being preserved as is"); } method = post; } // add eventual extra headers if (ref.getHeader() != null) { for (Iterator it = ref.getHeader().iterator(); it.hasNext();) { HeaderType header = (HeaderType) it.next(); method.setRequestHeader(header.getKey(), header.getValue()); } } int code = client.executeMethod(method); if (code == 200) { try { Header length = method.getResponseHeader("Content-Lenght"); if (maxSize > 0 && length != null && Long.parseLong(length.getValue()) > maxSize) { throw new WPSException( "Input " + getInputId() + " size " + length.getValue() + " exceeds maximum allowed size of " + maxSize + " according to HTTP Content-Lenght response header", "NoApplicableCode", getInputId()); } } catch (NumberFormatException e) { LOGGER.log(Level.FINE, "Failed to parse content lenght to check input limits respect, " + "moving on and checking data as it comes in", e); } input = method.getResponseBodyAsStream(); if (maxSize > 0) { input = new MaxSizeInputStream(input, getInputId(), maxSize); } } else { throw new WPSException("Error getting remote resources from " + ref.getHref() + ", http error " + code + ": " + method.getStatusText()); } } else { // use the normal url connection methods then... URLConnection conn = destination.openConnection(); conn.setConnectTimeout(timeout); conn.setReadTimeout(timeout); input = conn.getInputStream(); if (maxSize > 0) { input = new MaxSizeInputStream(input, getInputId(), maxSize); } } // actually parse the data if (input != null) { CancellingInputStream is = new CancellingInputStream(input, listener); return complexPPIO.decode(is); } else { throw new WPSException("Could not find a mean to read input " + inputId); } } finally { listener.progress(100); listener.complete(); // make sure to close the connection and streams no matter what if (refInput != null) { refInput.close(); } if (input != null) { input.close(); } if (method != null) { method.releaseConnection(); } if (refMethod != null) { refMethod.releaseConnection(); } } }
From source file:net.solarnetwork.node.io.url.UrlDataCollector.java
@Override public void collectData() { String resolvedUrl = url;//from w ww .j a v a 2 s .c o m if (urlFactory != null) { resolvedUrl = urlFactory.getObject(); } URL dataUrl = null; try { dataUrl = new URL(resolvedUrl); } catch (MalformedURLException e) { throw new RuntimeException("Bad url configured: " + resolvedUrl); } if (log.isDebugEnabled()) { log.debug("Connecting to URL [" + resolvedUrl + ']'); } BufferedReader reader = null; String data = null; String enc = null; Pattern pat = Pattern.compile(matchExpression); try { URLConnection conn = dataUrl.openConnection(); conn.setConnectTimeout(connectionTimeout); conn.setReadTimeout(connectionTimeout); conn.setUseCaches(false); InputStream in = conn.getInputStream(); if (this.encoding == null) { enc = conn.getContentEncoding(); if (enc != null) { if (log.isTraceEnabled()) { log.trace("Using connection encoding [" + enc + ']'); } this.encoding = enc; } } if (enc == null) { enc = getEncodingToUse(); } reader = new BufferedReader(new InputStreamReader(in, enc)); String lastLine = null; boolean keepGoing = true; while (keepGoing) { String line = reader.readLine(); if (line == null) { keepGoing = false; if (skipToLastLine) { line = lastLine; } } Matcher m = pat.matcher(line); if (m.find()) { if (log.isDebugEnabled()) { log.debug("Found matching data line [" + line + ']'); } data = line; keepGoing = false; } else { lastLine = line; } } } catch (IOException e) { throw new RuntimeException(e); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { if (log.isWarnEnabled()) { log.warn("IOException closing input stream: " + e); } } } } if (data == null) { log.info("Input stream finished without finding expected data"); } else { if (this.buffer == null) { this.buffer = new StringBuilder(data); } else { this.buffer.append(data); } } }
From source file:org.drools.core.io.impl.UrlResource.java
private URLConnection openURLConnection(URL url) throws IOException { URLConnection con = url.openConnection(); con.setConnectTimeout(TIMEOUT); con.setReadTimeout(TIMEOUT); return con;/*from w w w .j a v a2 s.c o m*/ }
From source file:net.solarnetwork.node.price.delimited.DelimitedPriceDatumDataSource.java
private String readDataRow(URL theUrl) { BufferedReader resp = null;/*from ww w.j a v a 2 s. co m*/ if (log.isDebugEnabled()) { log.debug("Requesting price data from [" + theUrl + ']'); } try { URLConnection conn = theUrl.openConnection(); conn.setConnectTimeout(this.connectionTimeout); conn.setReadTimeout(this.connectionTimeout); conn.setRequestProperty("Accept", "text/*"); resp = new BufferedReader(new InputStreamReader(conn.getInputStream())); String str; int skipCount = this.skipLines; while ((str = resp.readLine()) != null) { if (skipCount > 0) { skipCount--; continue; } break; } if (log.isTraceEnabled()) { log.trace("Found price data: " + str); } return str; } catch (IOException e) { throw new RuntimeException(e); } finally { if (resp != null) { try { resp.close(); } catch (IOException e) { // ignore this log.debug("Exception closing URL stream", e); } } } }