Example usage for java.net URLConnection setConnectTimeout

List of usage examples for java.net URLConnection setConnectTimeout

Introduction

In this page you can find the example usage for java.net URLConnection 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 static void main(String args[]) throws Exception {

    URL u = new URL("http://www.java2s.com");
    URLConnection uc = u.openConnection();

    uc.setConnectTimeout(1000);
    System.out.println(uc.getConnectTimeout());
}

From source file:org.dlut.mycloudserver.service.performancemonitor.PerformanceListener.java

public static void main(String[] args) {
    long t1 = System.currentTimeMillis();

    String url = "http://127.0.0.1:8001";
    URLConnection conn;
    try {/*from ww  w.  j  a  v a2 s .  c o  m*/
        conn = new URL(url).openConnection();
        conn.setConnectTimeout(1000);
        conn.setReadTimeout(1000);
        InputStream is = conn.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String res = br.readLine();
        br.close();
        is.close();
        JSONObject jsonRes = JSON.parseObject(res);
        double loadAverage = jsonRes.getDoubleValue("loadAverage");
        System.out.println(loadAverage);
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    long t2 = System.currentTimeMillis();

    System.out.println((t2 - t1) + "ms");
}

From source file:Main.java

private static void setTimeout(URLConnection conn, int milliseconds) {
    conn.setConnectTimeout(milliseconds);
    conn.setReadTimeout(milliseconds);/*from   w  ww  . jav  a  2  s.com*/
}

From source file:Main.java

public static InputStream HandlerData(String url) {
    InputStream inStream = null;/*from  ww  w . j a v  a  2  s. c  o  m*/

    try {
        URL feedUrl = new URL(url);
        URLConnection conn = feedUrl.openConnection();
        conn.setConnectTimeout(10 * 1000);
        inStream = conn.getInputStream();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return inStream;
}

From source file:org.bimserver.utils.NetUtils.java

public static String getContent(URL url, int timeOut) throws IOException {
    URLConnection openConnection = url.openConnection();
    openConnection.setConnectTimeout(timeOut);
    openConnection.setReadTimeout(timeOut);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    InputStream in = openConnection.getInputStream();
    IOUtils.copy(in, byteArrayOutputStream);
    in.close();/* www .j av  a 2s  .c o m*/
    return new String(byteArrayOutputStream.toByteArray(), Charsets.UTF_8);
}

From source file:org.bimserver.utils.NetUtils.java

public static byte[] getContentAsBytes(URL url, int timeOut) throws IOException {
    URLConnection openConnection = url.openConnection();
    openConnection.setConnectTimeout(timeOut);
    openConnection.setReadTimeout(timeOut);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    InputStream in = openConnection.getInputStream();
    IOUtils.copy(in, byteArrayOutputStream);
    in.close();//from ww w .j a  v a  2 s  .c  o  m
    return byteArrayOutputStream.toByteArray();
}

From source file:org.enderstone.server.uuid.ServerRequest.java

public static JSONObject parseDataFromURL(String connect) throws IOException {
    URL url = new URL(connect);
    URLConnection uc = url.openConnection();
    uc.setConnectTimeout(5000);
    uc.setReadTimeout(5000);/*from   w  w  w. j a v  a 2 s  .  com*/
    uc.connect();
    String encoding = uc.getContentEncoding();
    encoding = encoding == null ? "UTF-8" : encoding;
    try (Scanner scanner = new Scanner(uc.getInputStream(), encoding)) {
        scanner.useDelimiter("\\A");
        return new JSONObject(scanner.next());
    } catch (NoSuchElementException noskin) {
        return new JSONObject();
    }
}

From source file:Main.java

public static InputStream getInputStream(Context context, Uri uri) throws IOException {
    InputStream inputStream;/* www  .  j  a v a2 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.bdaum.zoom.net.core.internal.Activator.java

public static InputStream openStream(String url, int timeout)
        throws MalformedURLException, IOException, HttpException {
    URL u = new URL(url);
    URLConnection con = u.openConnection();
    con.setConnectTimeout(timeout);
    con.setReadTimeout(timeout);/*from  w  w w  .  j  a v a  2  s .  c  om*/
    try {
        return new BufferedInputStream(con.getInputStream());
    } catch (IOException e) {
        int responseCode = -1;
        if (con instanceof HttpURLConnection)
            responseCode = ((HttpURLConnection) con).getResponseCode();
        else if (con instanceof HttpsURLConnection)
            responseCode = ((HttpsURLConnection) con).getResponseCode();
        if (responseCode >= 0 && responseCode != HttpStatus.SC_OK)
            throw new HttpException(getStatusText(responseCode), e);
        throw e;
    }
}

From source file:skinsrestorer.utils.DataUtils.java

public static SkinProperty getProp(String id) throws IOException, ParseException, SkinFetchFailedException {
    URL url = new URL(skullbloburl + id + "?unsigned=false");
    URLConnection connection = url.openConnection();
    connection.setConnectTimeout(10000);
    connection.setReadTimeout(10000);//from   w w w.  ja v a 2  s  .  com
    connection.setUseCaches(false);
    InputStream is = connection.getInputStream();
    String result = IOUtils.toString(is, Charsets.UTF_8);
    IOUtils.closeQuietly(is);
    JSONArray properties = (JSONArray) ((JSONObject) new JSONParser().parse(result)).get("properties");
    for (int i = 0; i < properties.size(); i++) {
        JSONObject property = (JSONObject) properties.get(i);
        String name = (String) property.get("name");
        String value = (String) property.get("value");
        String signature = (String) property.get("signature");
        if (name.equals("textures")) {
            return new SkinProperty(name, value, signature);
        }
    }
    throw new SkinFetchFailedException(SkinFetchFailedException.Reason.NO_SKIN_DATA);
}