List of usage examples for java.net HttpURLConnection setConnectTimeout
public void setConnectTimeout(int timeout)
From source file:com.orchestra.portale.external.services.manager.CiRoService.java
@Override public String load() { try {//from w w w.j a v a2 s .c o m deletePois(); HttpURLConnection urlConnection = (HttpURLConnection) new URL(loadUrl).openConnection(); urlConnection.setConnectTimeout(15000); urlConnection.setReadTimeout(30000); String result = IOUtils.toString(urlConnection.getInputStream()); urlConnection.disconnect(); JsonParser parser = new JsonParser(); JsonElement json = parser.parse(result); JsonArray puntiCiro = json.getAsJsonObject().get("puntiCiro").getAsJsonArray(); StringBuilder insertedPoi = new StringBuilder(); for (int i = 0; i < puntiCiro.size(); i++) { insertedPoi.append(createPoi(i, puntiCiro)); } return insertedPoi.toString(); } catch (Exception e) { return "response{code:1,error:" + e.toString() + "}"; } }
From source file:com.notonthehighstreet.ratel.internal.utility.HttpRequest.java
public int call(final HttpURLConnection connection, final String method, final Map<String, String> headers, final Object body) throws IOException { connection.setConnectTimeout(TEN_SECONDS); connection.setReadTimeout(TEN_SECONDS); connection.setRequestMethod(method); for (final Map.Entry<String, String> header : headers.entrySet()) { connection.setRequestProperty(header.getKey(), header.getValue()); }/*from www . jav a2 s.c o m*/ connection.setDoOutput(true); mapper.writeValue(connection.getOutputStream(), body); return connection.getResponseCode(); }
From source file:com.eTilbudsavis.etasdk.network.impl.HttpURLNetwork.java
private HttpURLConnection openConnection(URL url, Request<?> request) throws IOException { HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(request.getTimeOut()); connection.setReadTimeout(request.getTimeOut()); connection.setUseCaches(false);//from ww w . j av a2s.c o m connection.setDoInput(true); // use caller-provided custom SslSocketFactory, if any, for HTTPS // if ("https".equals(url.getProtocol()) && mSslSocketFactory != null) { // ((HttpsURLConnection)connection).setSSLSocketFactory(mSslSocketFactory); // } return connection; }
From source file:org.piraso.replacer.spring.remoting.PirasoHttpInvokerRequestExecutorWithTimeout.java
protected void prepareConnection(HttpURLConnection con, int contentLength) throws IOException { super.prepareConnection(con, contentLength); if (context.isMonitored()) { con.setConnectTimeout(getConnectTimeoutMs() * 5); con.setReadTimeout(getReadTimeoutMs() * 5); } else {//ww w . ja va 2 s . co m con.setConnectTimeout(getConnectTimeoutMs()); con.setReadTimeout(getReadTimeoutMs()); } }
From source file:KV78Tester.java
public String[] getLines() throws Exception { String uri = "http://openov.nl:5078/line/"; URL url = new URL(uri); HttpURLConnection uc = (HttpURLConnection) url.openConnection(); uc.setRequestProperty("User-Agent", "KV78Turbo-Test"); uc.setConnectTimeout(60000); uc.setReadTimeout(60000);/*from ww w . jav a 2 s . c om*/ BufferedReader in; in = new BufferedReader(new InputStreamReader(uc.getInputStream(), "UTF-8")); try { return parseLines(in); } finally { uc.disconnect(); } }
From source file:cn.org.eshow.framwork.util.AbFileUtil.java
/** * ????.// ww w.j a v a 2 s .c om * * @param Url * @return int ? */ public static int getContentLengthFromUrl(String Url) { int mContentLength = 0; try { URL url = new URL(Url); HttpURLConnection mHttpURLConnection = (HttpURLConnection) url.openConnection(); mHttpURLConnection.setConnectTimeout(5 * 1000); mHttpURLConnection.setRequestMethod("GET"); mHttpURLConnection.setRequestProperty("Accept", "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"); mHttpURLConnection.setRequestProperty("Accept-Language", "zh-CN"); mHttpURLConnection.setRequestProperty("Referer", Url); mHttpURLConnection.setRequestProperty("Charset", "UTF-8"); mHttpURLConnection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"); mHttpURLConnection.setRequestProperty("Connection", "Keep-Alive"); mHttpURLConnection.connect(); if (mHttpURLConnection.getResponseCode() == 200) { // ???? mContentLength = mHttpURLConnection.getContentLength(); } } catch (Exception e) { e.printStackTrace(); AbLogUtil.d(AbFileUtil.class, "?" + e.getMessage()); } return mContentLength; }
From source file:cn.ctyun.amazonaws.internal.EC2MetadataClient.java
/** * Connects to the metadata service to read the specified resource and * returns the text contents.//ww w . j av a2 s. c om * * @param resourcePath * The resource * * @return The text payload returned from the Amazon EC2 Instance Metadata * service for the specified resource path. * * @throws IOException * If any problems were encountered while connecting to metadata * service for the requested resource path. * @throws AmazonClientException * If the requested metadata service is not found. */ public String readResource(String resourcePath) throws IOException, AmazonClientException { URL url = getEc2MetadataServiceUrlForResource(resourcePath); log.debug("Connecting to EC2 instance metadata service at URL: " + url.toString()); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(1000 * 2); connection.setRequestMethod("GET"); connection.setDoOutput(true); connection.connect(); return readResponse(connection); }
From source file:com.cannabiscoin.wallet.ExchangeRatesProvider.java
private static Map<String, ExchangeRate> requestExchangeRates(final URL url, final String userAgent, final String... fields) { final long start = System.currentTimeMillis(); HttpURLConnection connection = null; Reader reader = null;//ww w .java 2s.c om try { Double btcRate = 0.0; boolean cryptsyValue = true; Object result = getCoinValueBTC(); if (result == null) { result = getCoinValueBTC_BTER(); cryptsyValue = false; if (result == null) return null; } btcRate = (Double) result; connection = (HttpURLConnection) url.openConnection(); connection.setInstanceFollowRedirects(false); connection.setConnectTimeout(Constants.HTTP_TIMEOUT_MS); connection.setReadTimeout(Constants.HTTP_TIMEOUT_MS); connection.addRequestProperty("User-Agent", userAgent); connection.connect(); final int responseCode = connection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { reader = new InputStreamReader(new BufferedInputStream(connection.getInputStream(), 1024), Constants.UTF_8); final StringBuilder content = new StringBuilder(); Io.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(); if (!"timestamp".equals(currencyCode)) { final JSONObject o = head.getJSONObject(currencyCode); for (final String field : fields) { String rateStr = o.optString(field, null); if (rateStr != null) { try { double rateForBTC = Double.parseDouble(rateStr); rateStr = String.format("%.8f", rateForBTC * btcRate).replace(",", "."); final BigInteger rate = GenericUtils.toNanoCoins(rateStr, 0); if (rate.signum() > 0) { rates.put(currencyCode, new ExchangeRate(currencyCode, rate, url.getHost())); break; } } catch (final ArithmeticException x) { log.warn("problem fetching {} exchange rate from {}: {}", new Object[] { currencyCode, url, x.getMessage() }); } } } } } log.info("fetched exchange rates from {}, took {} ms", url, (System.currentTimeMillis() - start)); //Add Bitcoin information if (rates.size() == 0) { int i = 0; i++; } else { rates.put(CoinDefinition.cryptsyMarketCurrency, new ExchangeRate(CoinDefinition.cryptsyMarketCurrency, GenericUtils.toNanoCoins(String.format("%.8f", btcRate).replace(",", "."), 0), cryptsyValue ? "pubapi.cryptsy.com" : "data.bter.com")); rates.put("m" + CoinDefinition.cryptsyMarketCurrency, new ExchangeRate("m" + CoinDefinition.cryptsyMarketCurrency, GenericUtils.toNanoCoins( String.format("%.5f", btcRate * 1000).replace(",", "."), 0), cryptsyValue ? "pubapi.cryptsy.com" : "data.bter.com")); } return rates; } else { log.warn("http status {} when fetching {}", responseCode, url); } } catch (final Exception x) { log.warn("problem fetching exchange rates from " + url, x); } finally { if (reader != null) { try { reader.close(); } catch (final IOException x) { // swallow } } if (connection != null) connection.disconnect(); } return null; }
From source file:weavebytes.com.futureerp.activities.DashBoardActivity.java
public boolean isOnline() { ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo netInfo = cm.getActiveNetworkInfo(); if (netInfo != null && netInfo.isConnected()) { try {//w w w. j a v a2 s . c o m URL url = new URL("http://www.google.com"); HttpURLConnection urlc = (HttpURLConnection) url.openConnection(); urlc.setConnectTimeout(2000); urlc.connect(); if (urlc.getResponseCode() == 200) { return new Boolean(true); } } catch (MalformedURLException e1) { e1.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } return false; }
From source file:KV78Tester.java
public void checkLines(String[] lines) throws IOException { System.out.println("Checking " + lines.length + " lines"); for (int i = 0; i < lines.length; i++) { String uri = "http://openov.nl:5078/line/" + lines[i]; URL url = new URL(uri); HttpURLConnection uc = (HttpURLConnection) url.openConnection(); uc.setRequestProperty("User-Agent", "KV78Turbo-Test"); uc.setConnectTimeout(60000); uc.setReadTimeout(60000);//from www .ja va 2s . c o m BufferedReader in; in = new BufferedReader(new InputStreamReader(uc.getInputStream(), "UTF-8")); try { checkLines(in); } finally { uc.disconnect(); } } }