Example usage for java.net HttpURLConnection setReadTimeout

List of usage examples for java.net HttpURLConnection setReadTimeout

Introduction

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

Prototype

public void setReadTimeout(int timeout) 

Source Link

Document

Sets the read timeout to a specified timeout, in milliseconds.

Usage

From source file:com.samsung.msf.youtubeplayer.client.util.FeedParser.java

/**
 * Given a string representation of a URL, sets up a connection and gets an input stream.
 *//*  www. j a va  2s.  c o  m*/
public static InputStream downloadUrl(final URL url) throws IOException {
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setReadTimeout(NET_READ_TIMEOUT_MILLIS /* milliseconds */);
    conn.setConnectTimeout(NET_CONNECT_TIMEOUT_MILLIS /* milliseconds */);
    conn.setRequestMethod("GET");
    conn.setDoInput(true);
    // Starts the query
    conn.connect();
    return conn.getInputStream();
}

From source file:com.facebook.fresco.sample.urlsfetcher.ImageUrlsFetcher.java

@Nullable
private static String downloadContentAsString(String urlString) throws IOException {
    InputStream is = null;/*from ww  w. ja v a2 s.co m*/
    try {
        URL url = new URL(urlString);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestProperty("Authorization", IMGUR_CLIENT_ID);
        conn.setReadTimeout(10000 /* milliseconds */);
        conn.setConnectTimeout(15000 /* milliseconds */);
        conn.setRequestMethod("GET");
        conn.setDoInput(true);
        // Starts the query
        conn.connect();
        int response = conn.getResponseCode();
        if (response != HttpStatus.SC_OK) {
            FLog.e(TAG, "Album request returned %s", response);
            return null;
        }
        is = conn.getInputStream();
        return readAsString(is);
    } finally {
        if (is != null) {
            is.close();
        }
    }
}

From source file:Main.java

private static String getStringFromUrl(String url, int connectTimeout, int readTimeout)
        throws MalformedURLException, JSONException, IOException {
    URL urlObject = new URL(url);
    HttpURLConnection urlConn = (HttpURLConnection) urlObject.openConnection();
    String jsonString = "";

    if (connectTimeout != 0) {
        urlConn.setConnectTimeout(connectTimeout);
    }/*from  w w  w. ja  v a 2  s  .com*/
    if (readTimeout != 0) {
        urlConn.setReadTimeout(readTimeout);
    }

    try {
        jsonString = getStringFromInputStream(urlConn.getInputStream());
    } finally {
        urlConn.disconnect();
    }
    return jsonString;
}

From source file:circleplus.app.http.AbstractHttpApi.java

private static HttpURLConnection getHttpURLConnection(URL url, int requestMethod, boolean acceptJson)
        throws IOException {
    if (D)//ww  w . j  av a 2 s .co m
        Log.d(TAG, "execute method: " + requestMethod + " url: " + url.toString() + " accept JSON ?= "
                + acceptJson);

    String method;
    boolean isPost;
    switch (requestMethod) {
    case REQUEST_METHOD_GET:
        method = "GET";
        isPost = false;
        break;
    case REQUEST_METHOD_POST:
        method = "POST";
        isPost = true;
        break;
    default:
        method = "GET";
        isPost = false;
        break;
    }

    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setReadTimeout(TIMEOUT);
    conn.setConnectTimeout(TIMEOUT);
    conn.setRequestProperty("Content-Type", JSON_UTF8_CONTENT_TYPE);
    if (isPost && acceptJson) {
        conn.setRequestProperty("Accept", JSON_CONTENT_TYPE);
    }
    conn.setRequestMethod(method);
    /* setDoOutput(true) equals setRequestMethod("POST") */
    conn.setDoOutput(isPost);
    conn.setChunkedStreamingMode(0);
    // Starts the query
    conn.connect();

    return conn;
}

From source file:com.choices.imagecompare.urlsfetcher.ImageUrlsFetcher.java

@Nullable
private static String downloadContentAsString(String urlString) throws IOException {
    InputStream is = null;/*from w w  w  .  ja  va2  s  . co  m*/
    try {
        URL url = new URL(urlString);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestProperty("Authorization", IMGUR_CLIENT_ID);
        conn.setReadTimeout(10000 /* milliseconds */);
        conn.setConnectTimeout(15000 /* milliseconds */);
        conn.setRequestMethod("GET");
        conn.setDoInput(true);
        // Starts the query
        conn.connect();
        int response = conn.getResponseCode();
        if (response != SC_OK) {
            FLog.e(TAG, "Album request returned %s", response);
            return null;
        }
        is = conn.getInputStream();
        return readAsString(is);
    } finally {
        if (is != null) {
            is.close();
        }
    }
}

From source file:jfix.util.Urls.java

/**
 * Returns true if given url can be connected via HTTP within given timeout
 * (specified in seconds). Otherwise the url might be broken.
 *//*from ww w .j  av  a2  s  . c o m*/
public static boolean isConnectable(String url, int timeout) {
    try {
        URLConnection connection = new URL(url).openConnection();
        if (connection instanceof HttpURLConnection) {
            HttpURLConnection httpConnection = (HttpURLConnection) connection;
            httpConnection.setConnectTimeout(timeout * 1000);
            httpConnection.setReadTimeout(timeout * 1000);
            httpConnection.connect();
            int response = httpConnection.getResponseCode();
            httpConnection.disconnect();
            return response == HttpURLConnection.HTTP_OK;
        }
    } catch (Exception e) {
        return false;
    }
    return false;
}

From source file:jfix.util.Urls.java

/**
 * Returns the status code for connecting given url with given timeout.
 * Returns 0 if an IOException occurs./* w w w . j a v a 2s.c om*/
 */
public static int getStatus(String url, int timeout) {
    try {
        URLConnection connection = new URL(url).openConnection();
        if (connection instanceof HttpURLConnection) {
            HttpURLConnection httpConnection = (HttpURLConnection) connection;
            httpConnection.setConnectTimeout(timeout * 1000);
            httpConnection.setReadTimeout(timeout * 1000);
            httpConnection.connect();
            int response = httpConnection.getResponseCode();
            httpConnection.disconnect();
            return response;
        }
    } catch (IOException e) {
        // pass
    }
    return 0;
}

From source file:Main.java

public static String doGet(String urlStr) {
    URL url = null;//ww  w  .  j a  v  a  2s .  c om
    HttpURLConnection conn = null;
    InputStream is = null;
    ByteArrayOutputStream baos = null;
    try {
        url = new URL(urlStr);
        conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(TIMEOUT_IN_MILLIONS);
        conn.setConnectTimeout(TIMEOUT_IN_MILLIONS);
        conn.setRequestMethod("GET");
        conn.setRequestProperty("accept", "*/*");
        conn.setRequestProperty("connection", "Keep-Alive");
        if (conn.getResponseCode() == 200) {
            is = conn.getInputStream();
            baos = new ByteArrayOutputStream();
            int len = -1;
            byte[] buf = new byte[128];

            while ((len = is.read(buf)) != -1) {
                baos.write(buf, 0, len);
            }
            baos.flush();
            return baos.toString();
        } else {
            throw new RuntimeException(" responseCode is not 200 ... ");
        }

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (is != null)
                is.close();
        } catch (IOException e) {
        }
        try {
            if (baos != null)
                baos.close();
        } catch (IOException e) {
        }
        conn.disconnect();
    }

    return null;

}

From source file:Main.java

public static byte[] doGet(String urlStr) {
    URL url = null;/* w  w w. ja v a 2s.c o  m*/
    HttpURLConnection conn = null;
    InputStream is = null;
    ByteArrayOutputStream baos = null;
    try {
        url = new URL(urlStr);
        conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(TIMEOUT_IN_MILLIONS);
        conn.setConnectTimeout(TIMEOUT_IN_MILLIONS);
        conn.setRequestMethod("GET");
        conn.setRequestProperty("accept", "*/*");
        conn.setRequestProperty("connection", "Keep-Alive");
        if (conn.getResponseCode() == 200) {
            is = conn.getInputStream();
            baos = new ByteArrayOutputStream();
            int len = -1;
            byte[] buf = new byte[128];

            while ((len = is.read(buf)) != -1) {
                baos.write(buf, 0, len);
            }
            baos.flush();
            return baos.toByteArray();
        } else {
            throw new RuntimeException(" responseCode is not 200 ... ");
        }

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (is != null)
                is.close();
        } catch (IOException e) {
        }
        try {
            if (baos != null)
                baos.close();
        } catch (IOException e) {
        }
        conn.disconnect();
    }

    return null;

}

From source file:org.LinuxdistroCommunity.android.client.NetworkUtilities.java

public static String getAuthToken(String server, String code) {

    String token = null;/*from  www . j  a  v a  2 s  .co  m*/

    final ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair(PARAM_CODE, code));
    params.add(new BasicNameValuePair(PARAM_CLIENT_SECRET, CLIENT_SECRET));

    InputStream is = null;
    URL auth_token_url;
    try {
        auth_token_url = getRequestURL(server, PATH_OAUTH_ACCESS_TOKEN, params);
        Log.i(TAG, auth_token_url.toString());

        HttpURLConnection conn = (HttpURLConnection) auth_token_url.openConnection();
        conn.setReadTimeout(10000 /* milliseconds */);
        conn.setConnectTimeout(15000 /* milliseconds */);
        conn.setRequestMethod("GET");
        conn.setDoInput(true);
        // Starts the query
        conn.connect();
        int response_code = conn.getResponseCode();
        Log.d(TAG, "The response is: " + response_code);
        is = conn.getInputStream();

        // parse token_response as JSON to get the token out
        String str_response = readStreamToString(is, 500);
        Log.d(TAG, str_response);
        JSONObject response = new JSONObject(str_response);
        token = response.getString("access_token");

    } catch (MalformedURLException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        // Makes sure that the InputStream is closed after the app is
        // finished using it.
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    Log.i(TAG, token);
    return token;
}