List of usage examples for java.net HttpURLConnection setReadTimeout
public void setReadTimeout(int timeout)
From source file:org.craftercms.cstudio.publishing.StopServiceMain.java
public static void main(String[] args) throws Exception { FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext( "classpath:spring/shutdown-context.xml"); ReadablePropertyPlaceholderConfigurer properties = (ReadablePropertyPlaceholderConfigurer) context .getBean("cstudioShutdownProperties"); if (properties != null) { String url = getProperty(properties, PROP_URL); String path = getProperty(properties, PROP_SERVICE_PATH); String port = getProperty(properties, PROP_PORT); String password = URLEncoder.encode(getProperty(properties, PROP_PASSWORD), "UTF-8"); String target = url + ":" + port + path; if (LOGGER.isDebugEnabled()) { LOGGER.debug("Sending a stop request to " + target); }/*from www. ja v a 2s. c o m*/ target = target + "?" + StopServiceServlet.PARAM_PASSWORD + "=" + password; URL serviceUrl = new URL(target); HttpURLConnection connection = (HttpURLConnection) serviceUrl.openConnection(); connection.setRequestMethod("GET"); connection.setReadTimeout(10000); connection.connect(); try { connection.getContent(); } catch (ConnectException e) { // ignore this error (server will terminate as soon as the request is sent out) } } else { if (LOGGER.isErrorEnabled()) { LOGGER.error(PROPERTIES_NAME + " is not present in shutdown-context.xml"); } } context.close(); }
From source file:Main.java
public final static HttpURLConnection setTimeout(HttpURLConnection con) { con.setConnectTimeout(CONNECT_TIMEOUT_MSEC); con.setReadTimeout(READ_TIMEOUT_MSEC); return con;//from w ww . j a v a 2 s .co m }
From source file:Main.java
public static InputStream downloadURL(String link) throws IOException { URL url = new URL(link); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(10000); conn.setConnectTimeout(15000);//from w w w . j a va 2 s . com conn.setRequestMethod("GET"); conn.setDoInput(true); conn.connect(); logInfo("downloadStatus: " + conn.getResponseCode()); return conn.getInputStream(); }
From source file:Main.java
/** * Given a string url, connects and returns response code * * @param urlString string to fetch * @param readTimeOutMs read time out * @param connectionTimeOutMs connection time out * @param urlRedirect should use urlRedirect * @param useCaches should use cache * @return httpResponseCode http response code * @throws IOException/*from w w w. ja v a 2s .c o m*/ */ public static int checkUrlWithOptions(String urlString, int readTimeOutMs, int connectionTimeOutMs, boolean urlRedirect, boolean useCaches) throws IOException { URL url = new URL(urlString); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setReadTimeout(readTimeOutMs /* milliseconds */); connection.setConnectTimeout(connectionTimeOutMs /* milliseconds */); connection.setRequestMethod("GET"); connection.setInstanceFollowRedirects(urlRedirect); connection.setUseCaches(useCaches); // Starts the query connection.connect(); int responseCode = connection.getResponseCode(); connection.disconnect(); return responseCode; }
From source file:Main.java
private static HttpURLConnection getHttpURLConnection(String strURL) throws IOException { URL url = new URL(strURL); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(10000);//from ww w .j a v a 2 s. com conn.setReadTimeout(15000); return conn; }
From source file:Main.java
/** * Given a string url, connects and returns response code * * @param urlString string to fetch * @param network network// w w w. ja v a2 s .c om * @param readTimeOutMs read time out * @param connectionTimeOutMs connection time out * @param urlRedirect should use urlRedirect * @param useCaches should use cache * @return httpResponseCode http response code * @throws IOException */ @TargetApi(LOLLIPOP) public static int checkUrlWithOptionsOverNetwork(String urlString, Network network, int readTimeOutMs, int connectionTimeOutMs, boolean urlRedirect, boolean useCaches) throws IOException { if (network == null) { return -1; } URL url = new URL(urlString); HttpURLConnection connection = (HttpURLConnection) network.openConnection(url); connection.setReadTimeout(readTimeOutMs /* milliseconds */); connection.setConnectTimeout(connectionTimeOutMs /* milliseconds */); connection.setRequestMethod("GET"); connection.setInstanceFollowRedirects(urlRedirect); connection.setUseCaches(useCaches); // Starts the query connection.connect(); int responseCode = connection.getResponseCode(); connection.disconnect(); return responseCode; }
From source file:Main.java
private static HttpURLConnection createHttpsURLConnection(String urlString) throws IOException { URL url = new URL(urlString); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(ONE_MINUTE); connection.setReadTimeout(ONE_MINUTE); return connection; }
From source file:Main.java
public static Document getUrlAsDocument(String urlAsString, int timeout) throws Exception { URL url = new URL(urlAsString); //using proxy may increase latency HttpURLConnection hConn = (HttpURLConnection) url.openConnection(); hConn.setReadTimeout(timeout); hConn.setConnectTimeout(timeout);// w w w .j a v a2 s . c o m // hConn.setRequestProperty("Accept-Encoding", "gzip, deflate"); InputStream is = hConn.getInputStream(); // if ("gzip".equals(hConn.getContentEncoding())) // is = new GZIPInputStream(is); return newDocumentBuilder().parse(is); }
From source file:Main.java
public static Reader getUri(URL url) throws IOException { //Log.d(TAG, "getUri: " + url.toString()); boolean useGzip = false; HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(30 * 1000); conn.setRequestProperty("Accept-Encoding", "gzip"); conn.connect();/*from ww w. java2 s.co m*/ InputStream in = conn.getInputStream(); final Map<String, List<String>> headers = conn.getHeaderFields(); // This is a map, but we can't assume the key we're looking for // is in normal casing. So it's really not a good map, is it? final Set<Map.Entry<String, List<String>>> set = headers.entrySet(); for (Iterator<Map.Entry<String, List<String>>> i = set.iterator(); i.hasNext();) { Map.Entry<String, List<String>> entry = i.next(); if ("Content-Encoding".equalsIgnoreCase(entry.getKey())) { for (Iterator<String> j = entry.getValue().iterator(); j.hasNext();) { String str = j.next(); if (str.equalsIgnoreCase("gzip")) { useGzip = true; break; } } // Break out of outer loop. if (useGzip) { break; } } } if (useGzip) { return new BufferedReader(new InputStreamReader(new GZIPInputStream(in)), 8 * 1024); } else { return new BufferedReader(new InputStreamReader(in), 8 * 1024); } }
From source file:Main.java
/** * Pings a HTTP URL. This effectively sends a HEAD request and returns <code>true</code> if the response code is in * the 200-399 range.//from w w w . j av a 2 s . c o m * @param url The HTTP URL to be pinged. * @param timeout The timeout in millis for both the connection timeout and the response read timeout. Note that * the total timeout is effectively two times the given timeout. * @return <code>true</code> if the given HTTP URL has returned response code 200-399 on a HEAD request within the * given timeout, otherwise <code>false</code>. */ public static boolean ping(String url, int timeout) { // Otherwise an exception may be thrown on invalid SSL certificates: url = url.replaceFirst("^https", "http"); try { HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); connection.setConnectTimeout(timeout); connection.setReadTimeout(timeout); connection.setRequestMethod("HEAD"); int responseCode = connection.getResponseCode(); return (200 <= responseCode && responseCode <= 399); } catch (IOException exception) { return false; } }