Example usage for java.net HttpURLConnection setConnectTimeout

List of usage examples for java.net HttpURLConnection setConnectTimeout

Introduction

In this page you can find the example usage for java.net HttpURLConnection setConnectTimeout.

Prototype

public void setConnectTimeout(int timeout) 

Source Link

Document

Sets a specified timeout value, in milliseconds, to be used when opening a communications link to the resource referenced by this URLConnection.

Usage

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  w w.j a  v a2 s .c o  m
}

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);
    conn.setReadTimeout(15000);//from   w w  w  .j a va 2  s .  c o m
    return conn;
}

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

/** 
 * Get image from newwork //from  ww  w .j av a2  s  .c  om
 * @param path The path of image 
 * @return InputStream 
 * @throws Exception 
 */
public static InputStream getImageStream(String path) throws Exception {
    URL url = new URL(path);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setConnectTimeout(5 * 1000);
    conn.setRequestMethod("GET");
    if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
        return conn.getInputStream();
    }
    return null;
}

From source file:Main.java

public static boolean isInternetWorking() {
    boolean success = false;
    try {/*  w w  w  .j a va  2 s .c o  m*/
        URL url = new URL("https://google.com");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setConnectTimeout(8000);
        connection.connect();
        success = connection.getResponseCode() == 200;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return success;
}

From source file:Main.java

public static boolean isInternetWorking() {
    boolean success = false;
    try {/* www. j  a  v a  2 s.  c  om*/
        URL url = new URL("https://google.com");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setConnectTimeout(10000);
        connection.connect();
        success = connection.getResponseCode() == 200;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return success;
}

From source file:Main.java

public static Bitmap getHttpBitmap(String url) {
    URL myFileUrl = null;/*from   ww  w . j a v a2s.co m*/
    Bitmap bitmap = null;
    try {
        myFileUrl = new URL(url);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    try {
        HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
        conn.setConnectTimeout(0);
        conn.setDoInput(true);
        conn.connect();
        InputStream is = conn.getInputStream();
        bitmap = BitmapFactory.decodeStream(is);
        is.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bitmap;
}

From source file:Main.java

public static InputStream getRequest(String path) throws Exception {
    URL url = new URL(path);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("GET");
    conn.setConnectTimeout(5000);
    if (conn.getResponseCode() == 200) {
        return conn.getInputStream();
    }/*from   www.  j a va  2  s  .c  o m*/
    return null;
}

From source file:Main.java

public static void downloadResource(final String fileUrl) {
    new Thread(new Runnable() {

        @Override//from w w  w.j  ava  2  s. c  o  m
        public void run() {
            inStream = null;
            isOver = false;
            try {
                URL url = new URL(fileUrl);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setConnectTimeout(5000);
                inStream = conn.getInputStream();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            isOver = true;
        }
    }).start();
}

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 .  ja v 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;
    }
}