Example usage for java.net URLConnection setReadTimeout

List of usage examples for java.net URLConnection setReadTimeout

Introduction

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

Prototype

public void setReadTimeout(int timeout) 

Source Link

Document

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

Usage

From source file:ee.ria.xroad.common.conf.globalconf.ConfigurationLocation.java

/**
 * @return the connection used to connect to the download url
 * @throws IOException//  w  ww . j av  a2  s  .com
 */
public static URLConnection getDownloadURLConnection(String urlStr) throws IOException {
    URL url = new URL(urlStr);
    URLConnection connection = url.openConnection();
    connection.setReadTimeout(READ_TIMEOUT);
    return connection;
}

From source file:net.foxgenesis.helper.SiteReader.java

private static BufferedReader getStream(URL url) throws IOException {
    URLConnection connection = url.openConnection();
    connection.setRequestProperty("User-Agent", "X-ServerStats");
    connection.setReadTimeout(5000);
    return new BufferedReader(new InputStreamReader(connection.getInputStream()));
}

From source file:skinsrestorer.shared.utils.MojangAPI.java

private static URLConnection setupConnection(URL url) throws IOException {
    URLConnection connection = url.openConnection();
    connection.setConnectTimeout(10000);
    connection.setReadTimeout(10000);
    connection.setUseCaches(false);// w  ww.j ava  2s .  co  m
    connection.setDoInput(true);
    connection.setDoOutput(true);
    return connection;
}

From source file:cz.ceskaexpedice.k5.k5jaas.basic.K5LoginModule.java

public static URLConnection openConnection(String urlString, String user, String pass)
        throws MalformedURLException, IOException {
    URL url = new URL(urlString);
    String userPassword = user + ":" + pass;
    String encoded = new String(Base64Coder.encode(userPassword.getBytes()));
    URLConnection uc = url.openConnection();
    uc.setReadTimeout(1000);
    uc.setConnectTimeout(1000);/*from   ww  w  .j a v a  2s  .  co  m*/
    uc.setRequestProperty("Authorization", "Basic " + encoded);
    return uc;
}

From source file:Main.java

public static InputStream getInputStream(Context context, Uri uri) throws IOException {
    InputStream inputStream;//  ww w. j  av  a  2 s.c o  m
    if (ContentResolver.SCHEME_ANDROID_RESOURCE.equals(uri.getScheme())
            || ContentResolver.SCHEME_ANDROID_RESOURCE.equals(uri.getScheme())
            || ContentResolver.SCHEME_FILE.equals(uri.getScheme())) {
        inputStream = context.getContentResolver().openInputStream(uri);
    } else {
        URLConnection urlConnection = new URL(uri.toString()).openConnection();
        urlConnection.setConnectTimeout(URLCONNECTION_CONNECTION_TIMEOUT_MS);
        urlConnection.setReadTimeout(URLCONNECTION_READ_TIMEOUT_MS);
        inputStream = urlConnection.getInputStream();
    }
    return new BufferedInputStream(inputStream);
}

From source file:com.netsteadfast.greenstep.util.WsServiceUtils.java

public static boolean testConnection(String wsdlAddress) throws Exception {
    if (StringUtils.isBlank(wsdlAddress)) {
        return true;
    }/*from ww w. jav  a2s .  c  o m*/
    boolean status = false;
    try {
        URL url = new URL(wsdlAddress);
        URLConnection connection = url.openConnection();
        connection.setConnectTimeout(TIMEOUT);
        connection.setReadTimeout(TIMEOUT);
        if (connection.getContent() != null) {
            status = true;
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return status;
}

From source file:com.adrguides.utils.HTTPUtils.java

public static InputStream openAddress(Context context, URL url) throws IOException {
    if ("file".equals(url.getProtocol()) && url.getPath().startsWith("/android_asset/")) {
        return context.getAssets().open(url.getPath().substring(15)); // "/android_asset/".length() == 15
    } else {/*www .  ja v  a 2s. co m*/
        URLConnection urlconn = url.openConnection();
        urlconn.setReadTimeout(10000 /* milliseconds */);
        urlconn.setConnectTimeout(15000 /* milliseconds */);
        urlconn.setAllowUserInteraction(false);
        urlconn.setDoInput(true);
        urlconn.setDoOutput(false);
        if (urlconn instanceof HttpURLConnection) {
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            int responsecode = connection.getResponseCode();
            if (responsecode != HttpURLConnection.HTTP_OK) {
                throw new IOException("Http response code returned:" + responsecode);
            }
        }
        return urlconn.getInputStream();
    }
}

From source file:fuliao.fuliaozhijia.data.UserData.java

public static String download(String mediaId, String dir) {
    //      http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=ACCESS_TOKEN&media_id=MEDIA_ID
    URL source;//from w  ww. j  ava  2  s.  co m
    try {
        source = new URL("http://file.api.weixin.qq.com/cgi-bin/media/get?access_token="
                + "oPaVk_NjH_TCX1fQhIVz_8DoRqE85Vx2E_sawJWQXpen5Q6HykjRnqA--6yE-y2VaQTU1f3vY5K-udylcgm55igkwa--7kVQ-KyDndcylmE"
                //WeixinAccessUtil.getAccessToken()
                + "&media_id=" + mediaId);
        URLConnection connection = source.openConnection();
        connection.setConnectTimeout(60 * 1000);
        connection.setReadTimeout(300 * 1000);
        InputStream input = connection.getInputStream();
        String fileType = connection.getHeaderField("Content-disposition");
        System.out.println("Content-disposition:" + fileType);
        String fileName = UUID.randomUUID().toString()
                + fileType.substring(fileType.lastIndexOf("."), fileType.length() - 1);
        File file = new File(dir + fileName);
        FileUtils.copyInputStreamToFile(input, file);
        return fileName;
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.apache.hadoop.hdfs.web.URLConnectionFactory.java

/**
 * Sets timeout parameters on the given URLConnection.
 * /*from  ww  w  .ja va2s. c  o m*/
 * @param connection
 *          URLConnection to set
 * @param socketTimeout
 *          the connection and read timeout of the connection.
 */
private static void setTimeouts(URLConnection connection, int socketTimeout) {
    connection.setConnectTimeout(socketTimeout);
    connection.setReadTimeout(socketTimeout);
}

From source file:com.baidu.api.client.core.DownloadUtil.java

/**
 * Download the file pointed by the url and return the content as byte array.
 *
 * @param strUrl         The Url to be downloaded.
 * @param connectTimeout Connect timeout in milliseconds.
 * @param readTimeout    Read timeout in milliseconds.
 * @param maxFileSize    Max file size in BYTE.
 * @return The file content as byte array.
 *///from  w  ww  .j a  va 2  s  .c  o m
public static byte[] downloadFile(String strUrl, int connectTimeout, int readTimeout, int maxFileSize) {
    InputStream in = null;
    try {
        URL url = new URL(strUrl); // URL?
        URLConnection ucon = url.openConnection();
        ucon.setConnectTimeout(connectTimeout);
        ucon.setReadTimeout(readTimeout);
        ucon.connect();
        if (ucon.getContentLength() > maxFileSize) {
            String msg = "File " + strUrl + " size [" + ucon.getContentLength()
                    + "] too large, download stoped.";
            throw new ClientInternalException(msg);
        }
        if (ucon instanceof HttpURLConnection) {
            HttpURLConnection httpCon = (HttpURLConnection) ucon;
            if (httpCon.getResponseCode() > 399) {
                String msg = "Failed to download file " + strUrl + " server response "
                        + httpCon.getResponseMessage();
                throw new ClientInternalException(msg);
            }
        }
        in = ucon.getInputStream(); // ?
        byte[] byteBuf = new byte[BUFFER_SIZE];
        byte[] ret = null;
        int count, total = 0;
        // ??
        while ((count = in.read(byteBuf, total, BUFFER_SIZE - total)) > 0) {
            total += count;
            if (total + 124 >= BUFFER_SIZE)
                break;
        }
        if (total < BUFFER_SIZE - 124) {
            ret = ArrayUtils.subarray(byteBuf, 0, total);
        } else {
            ByteArrayOutputStream bos = new ByteArrayOutputStream(MATERIAL_SIZE);
            count = total;
            total = 0;
            do {
                bos.write(byteBuf, 0, count);
                total += count;
                if (total > maxFileSize) {
                    String msg = "File " + strUrl + " size exceed [" + maxFileSize + "] download stoped.";
                    throw new ClientInternalException(msg);
                }
            } while ((count = in.read(byteBuf)) > 0);
            ret = bos.toByteArray();
        }
        if (ret.length < MIN_SIZE) {
            String msg = "File " + strUrl + " size [" + maxFileSize + "] too small.";
            throw new ClientInternalException(msg);
        }
        return ret;
    } catch (IOException e) {
        String msg = "Failed to download file " + strUrl + " msg=" + e.getMessage();
        throw new ClientInternalException(msg);
    } finally {
        try {
            if (in != null)
                in.close();
        } catch (IOException e) {
            // ?
            System.out.println("Exception while close url - " + e.getMessage());
        }
    }
}