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:com.bt.download.android.gui.transfers.HttpDownload.java

static void simpleHTTP(String url, OutputStream out, int timeout) throws Throwable {
    URL u = new URL(url);
    URLConnection con = u.openConnection();
    con.setConnectTimeout(timeout);
    con.setReadTimeout(timeout);//from   w  w w. j a  v a2s. co  m
    InputStream in = con.getInputStream();
    try {

        byte[] b = new byte[1024];
        int n = 0;
        while ((n = in.read(b, 0, b.length)) != -1) {
            out.write(b, 0, n);
        }
    } finally {
        try {
            out.close();
        } catch (Throwable e) {
            // ignore   
        }
        try {
            in.close();
        } catch (Throwable e) {
            // ignore   
        }
    }
}

From source file:org.royaldev.royalcommands.VUUpdater.java

/**
 * Gets the update information from the title of the latest file available from the CurseForge API.
 *
 * @param pluginID ID of the plugin to get the information from. The ID comes from CurseForge.
 * @return {@link org.royaldev.royalcommands.VUUpdater.VUUpdateInfo}
 * @throws IOException If any errors occur
 *//*  w w w .ja  va  2s . com*/
public static VUUpdateInfo getUpdateInfo(String pluginID) throws IOException {
    final URLConnection conn = new URL("https://api.curseforge.com/servermods/files?projectIds=" + pluginID)
            .openConnection();
    conn.setConnectTimeout(5000);
    conn.setRequestProperty("User-Agent", "VU/1.0");
    conn.setDoOutput(true);
    final BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    final String response = reader.readLine();
    final JSONArray array = (JSONArray) JSONValue.parse(response);
    final Matcher m = VUUpdater.vuPattern
            .matcher((String) ((JSONObject) array.get(array.size() - 1)).get("name"));
    if (!m.matches())
        throw new IllegalArgumentException("No match found!");
    final byte[] vuBytes = VUUpdater.hexStringToByteArray(m.group(4));
    return new VUUpdateInfo(m.group(2), vuBytes);
}

From source file:crow.util.Util.java

public static String urlGet(String url) throws IOException {
    URLConnection conn = new URL(url).openConnection();
    conn.setConnectTimeout(20000);
    InputStream input = conn.getInputStream();
    String result = inputStreamToString(input);
    return result;
}

From source file:com.entertailion.android.shapeways.api.ShapewaysClient.java

/**
 * Utility method to create a URL connection
 * /*from w  ww .  ja va2  s. c  o m*/
 * @param urlValue
 * @param doPost
 *            is this for a HTTP POST
 * @return
 * @throws Exception
 */
private static URLConnection getUrlConnection(String urlValue, boolean doPost) throws Exception {
    URL url = new URL(urlValue);
    URLConnection urlConnection = url.openConnection();
    urlConnection.setConnectTimeout(0);
    urlConnection.setReadTimeout(0);
    if (doPost) {
        urlConnection.setDoOutput(true);
    }
    return urlConnection;
}

From source file:com.frostwire.transfers.BaseHttpDownload.java

static void simpleHTTP(String url, OutputStream out, int timeout) throws Throwable {
    URL u = new URL(url);
    URLConnection con = u.openConnection();
    con.setConnectTimeout(timeout);
    con.setReadTimeout(timeout);/* w ww  .  ja va 2  s .c o  m*/
    InputStream in = con.getInputStream();
    try {

        byte[] b = new byte[1024];
        int n = 0;
        while ((n = in.read(b, 0, b.length)) != -1) {
            out.write(b, 0, n);
        }
    } finally {
        try {
            out.close();
        } catch (Throwable e) {
            // ignore
        }
        try {
            in.close();
        } catch (Throwable e) {
            // ignore
        }
    }
}

From source file:org.arasthel.almeribus.utils.LoadFromWeb.java

public static ResultTiempo calcularTiempo(Context context, int parada, int linea) {
    ResultTiempo result = new ResultTiempo();
    if (!isConnectionEnabled(context)) {
        Log.d("AlmeriBus", "No hay conexin");
        result.setTiempo(ERROR_IO);// w w  w  . j av a 2s .c om
    }
    try {
        loadCookie();
        URL url = new URL(QUERY_ADDRESS_TIEMPO_PARADA + linea + "/" + parada + "/" + "3B5579C8FFD6");
        URLConnection connection = url.openConnection();
        connection.setConnectTimeout(10000);
        connection.setReadTimeout(15000);
        connection.setRequestProperty("REFERER", "http://m.surbus.com/tiempo-espera/");
        connection.setRequestProperty("Content-Type", "application/json;charset=utf-8");
        connection.setRequestProperty("X-Requested-With", "XMLHttpRequest");
        connection.setRequestProperty("Cookie", "ASP.NET_SessionId=" + cookie);
        Scanner scan = new Scanner(connection.getInputStream());
        StringBuilder strBuilder = new StringBuilder();
        while (scan.hasNextLine()) {
            strBuilder.append(scan.nextLine());
        }
        scan.close();
        Log.d("Almeribus", strBuilder.toString());
        JSONObject json = new JSONObject(strBuilder.toString());
        boolean isSuccessful = json.getBoolean("success");
        int type = json.getInt("waitTimeType");
        if (isSuccessful && type > 0) {
            int time = json.getInt("waitTime");
            if (time == Integer.MAX_VALUE) {
                time = NO_DATOS;
            }
            if (time <= 0) {
                time = 0;
            }
            result.setTiempo(time);
            result.setTiempoTexto(json.getString("waitTimeString"));
        } else {
            result.setTiempo(NO_DATOS);
        }
    } catch (Exception e) {
        Log.d("Almeribus", e.toString());
        result.setTiempo(ERROR_IO);
        return result;
    }
    return result;
}

From source file:org.spoutcraft.launcher.util.Utils.java

public static boolean pingURL(String urlLoc) {
    InputStream stream = null;//from w w w  .  j a  v a  2 s.c o m
    try {
        final URL url = new URL(urlLoc);
        final URLConnection conn = url.openConnection();
        conn.setConnectTimeout(10000);
        stream = conn.getInputStream();
        return true;
    } catch (IOException e) {
        return false;
    } finally {
        IOUtils.closeQuietly(stream);
    }
}

From source file:com.comcast.cdn.traffic_control.traffic_monitor.util.Fetcher.java

public static String fetchContent(final String stateUrl, final String ipStr, final int port, final int timeout)
        throws IOException {
    final URLConnection conn = new URL(stateUrl)
            .openConnection(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ipStr, port)));
    if (timeout != 0) {
        conn.setConnectTimeout(timeout);
        conn.setReadTimeout(timeout);/*from  w  w w.  j av a  2s. c o m*/
    }
    conn.connect();

    return IOUtils.toString(new InputStreamReader(conn.getInputStream(), UTF8_STR));
}

From source file:ru.codeinside.gses.webui.form.JsonForm.java

static String loadTemplate(ActivitiApp app, String ref) {
    try {//from   w ww  .  ja v a  2s .  c  o  m
        URL serverUrl = app.getServerUrl();
        URL url = new URL(serverUrl, ref);
        logger().info("fetch template " + url);
        URLConnection connection = url.openConnection();
        connection.setDoOutput(false);
        connection.setDoInput(true);
        connection.setConnectTimeout(5000);
        connection.setReadTimeout(5000);
        connection.setUseCaches(false);
        connection.connect();
        return Streams.toString(connection.getInputStream());
    } catch (MalformedURLException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.arasthel.almeribus.utils.LoadFromWeb.java

public static String getMensajeDesarrollador() {
    String mensaje = null;//from ww w .  j  av  a2  s .  c o  m
    try {
        URL url = new URL("http://arasthel.byethost14.com/almeribus/message.html?token="
                + new Random().nextInt(Integer.MAX_VALUE));
        URLConnection connection = url.openConnection();
        connection.setUseCaches(false);
        connection.setConnectTimeout(10000);
        connection.setReadTimeout(15000);
        Scanner scan = new Scanner(connection.getInputStream());
        StringBuilder strBuilder = new StringBuilder();
        while (scan.hasNextLine()) {
            strBuilder.append(scan.nextLine());
        }
        scan.close();
        mensaje = strBuilder.toString();
    } catch (Exception e) {

    }
    return mensaje;
}