List of usage examples for java.net URLConnection setReadTimeout
public void setReadTimeout(int timeout)
From source file:ninja.standalone.NinjaJettyTest.java
static public String get(String url) throws Exception { URL u = new URL(url); URLConnection conn = u.openConnection(); conn.setAllowUserInteraction(false); conn.setConnectTimeout(3000);/* w w w . j a v a 2 s. com*/ conn.setReadTimeout(3000); try (InputStream is = conn.getInputStream()) { return IOUtils.toString(conn.getInputStream()); } }
From source file:ec.edu.ucuenca.dcc.sld.HttpUtils.java
public static synchronized String Http(String s) throws SQLException, IOException { String resp = ""; 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"; }/* ww w. ja v a 2 s.c om*/ reader.close(); return resp; }
From source file:com.evilisn.DAO.CertMapper.java
static void setTimeout(URLConnection conn) { conn.setConnectTimeout(10 * 1000); conn.setReadTimeout(10 * 1000); }
From source file:dataflow.feed.api.Weather.java
/** * Method which builds the string of the URL to call * @param myURL the URL which must become a callURL * @return //from w w w. ja v a 2 s .c om */ public static String callURL(String myURL) { //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); 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) { throw new RuntimeException("Exception while calling URL:" + myURL, e); } return sb.toString(); }
From source file:org.wso2.carbon.apimgt.core.util.APIMWSDLUtils.java
/** * Retrieves the WSDL located in the provided URI ({@code wsdlUrl} * * @param wsdlUrl URL of the WSDL file//w ww . j a va 2 s .co m * @return Content bytes of the WSDL file * @throws APIMgtWSDLException If an error occurred while retrieving the WSDL file */ public static byte[] getWSDL(String wsdlUrl) throws APIMgtWSDLException { ByteArrayOutputStream outputStream = null; InputStream inputStream = null; URLConnection conn; try { URL url = new URL(wsdlUrl); conn = url.openConnection(); conn.setConnectTimeout(CONNECTION_TIMEOUT); conn.setReadTimeout(READ_TIMEOUT); conn.connect(); outputStream = new ByteArrayOutputStream(); inputStream = conn.getInputStream(); IOUtils.copy(inputStream, outputStream); return outputStream.toByteArray(); } catch (IOException e) { throw new APIMgtWSDLException("Error while reading content from " + wsdlUrl, e, ExceptionCodes.INVALID_WSDL_URL_EXCEPTION); } finally { if (outputStream != null) { IOUtils.closeQuietly(outputStream); } if (inputStream != null) { IOUtils.closeQuietly(inputStream); } } }
From source file:net.technicpack.launchercore.restful.RestObject.java
public static <T extends RestObject> T getRestObject(Class<T> restObject, String url) throws RestfulAPIException { InputStream stream = null;/*w w w . j a v a2s.c o m*/ try { URLConnection conn = new URL(url).openConnection(); conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2"); conn.setConnectTimeout(15000); conn.setReadTimeout(15000); stream = conn.getInputStream(); String data = IOUtils.toString(stream); T result = gson.fromJson(data, restObject); if (result == null) { throw new RestfulAPIException("Unable to access URL [" + url + "]"); } if (result.hasError()) { throw new RestfulAPIException("Error in response: " + result.getError()); } return result; } catch (SocketTimeoutException e) { throw new RestfulAPIException("Timed out accessing URL [" + url + "]", e); } catch (MalformedURLException e) { throw new RestfulAPIException("Invalid URL [" + url + "]", e); } catch (JsonParseException e) { throw new RestfulAPIException("Error parsing response JSON at URL [" + url + "]", e); } catch (IOException e) { throw new RestfulAPIException("Error accessing URL [" + url + "]", e); } finally { IOUtils.closeQuietly(stream); } }
From source file:de.schildbach.wallet.litecoin.ExchangeRatesProvider.java
private static Double getLycBtcRate() { try {/*from www .j a v a 2s . co m*/ final URL URL = new URL("http://pubapi.cryptsy.com/api.php?method=singlemarketdata&marketid=177"); final URLConnection connection = URL.openConnection(); connection.setConnectTimeout(TIMEOUT_MS); connection.setReadTimeout(TIMEOUT_MS); connection.connect(); final StringBuilder content = new StringBuilder(); Reader reader = null; try { reader = new InputStreamReader(new BufferedInputStream(connection.getInputStream(), 1024)); IOUtils.copy(reader, content); final JSONObject head = new JSONObject(content.toString()); final JSONObject o = head.getJSONObject("return").getJSONObject("markets").getJSONObject("LYC"); final Double rate = o.getDouble("lasttradeprice"); if (rate != null) return rate; } finally { if (reader != null) reader.close(); } } catch (final IOException x) { x.printStackTrace(); } catch (final JSONException x) { x.printStackTrace(); } return null; }
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 ww w .jav a 2 s . com 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:org.apache.niolex.commons.net.DownloadUtil.java
/** * Download the file pointed by the url and return the content as byte array. * * @param strUrl// w w w . j ava2 s. c o m * The Url to be downloaded. * @param connectTimeout * Connect timeout in milliseconds. * @param readTimeout * Read timeout in milliseconds. * @param maxFileSize * Max file size in BYTE. * @param useCache Whether we use thread local cache or not. * @return The file content as byte array. * @throws NetException */ public static byte[] downloadFile(String strUrl, int connectTimeout, int readTimeout, int maxFileSize, Boolean useCache) throws NetException { LOG.debug("Start to download file [{}], C{}R{}M{}.", strUrl, connectTimeout, readTimeout, maxFileSize); InputStream in = null; try { URL url = new URL(strUrl); // We use Java URL to download file. URLConnection ucon = url.openConnection(); ucon.setConnectTimeout(connectTimeout); ucon.setReadTimeout(readTimeout); ucon.setDoOutput(false); ucon.setDoInput(true); ucon.connect(); final int contentLength = ucon.getContentLength(); validateContentLength(strUrl, contentLength, maxFileSize); if (ucon instanceof HttpURLConnection) { validateHttpCode(strUrl, (HttpURLConnection) ucon); } in = ucon.getInputStream(); // Get the input stream. byte[] ret = null; // Create the byte array buffer according to the strategy. if (contentLength > 0) { ret = commonDownload(contentLength, in); } else { ret = unusualDownload(strUrl, in, maxFileSize, useCache); } LOG.debug("Succeeded to download file [{}] size {}.", strUrl, ret.length); return ret; } catch (NetException e) { LOG.info(e.getMessage()); throw e; } catch (Exception e) { String msg = "Failed to download file " + strUrl + " msg=" + e.toString(); LOG.warn(msg); throw new NetException(NetException.ExCode.IOEXCEPTION, msg, e); } finally { // Close the input stream. StreamUtil.closeStream(in); } }
From source file:de.schildbach.wallet.litecoin.ExchangeRatesProvider.java
private static Map<String, ExchangeRate> getBlockchainInfo() { try {/*from w w w . j a va2 s . c o m*/ final URL URL = new URL("https://blockchain.info/ticker"); final URLConnection connection = URL.openConnection(); connection.setConnectTimeout(TIMEOUT_MS); connection.setReadTimeout(TIMEOUT_MS); connection.connect(); final StringBuilder content = new StringBuilder(); Reader reader = null; try { reader = new InputStreamReader(new BufferedInputStream(connection.getInputStream(), 1024)); IOUtils.copy(reader, content); final Map<String, ExchangeRate> rates = new TreeMap<String, ExchangeRate>(); final JSONObject head = new JSONObject(content.toString()); for (final Iterator<String> i = head.keys(); i.hasNext();) { final String currencyCode = i.next(); final JSONObject o = head.getJSONObject(currencyCode); final Double drate = o.getDouble("15m") * LycBtcRate; String rate = String.format("%.8f", drate); rate = rate.replace(",", "."); if (rate != null) rates.put(currencyCode, new ExchangeRate(currencyCode, Utils.toNanoCoins(rate), "cryptsy.com and " + URL.getHost())); } return rates; } finally { if (reader != null) reader.close(); } } catch (final IOException x) { x.printStackTrace(); } catch (final JSONException x) { x.printStackTrace(); } return null; }