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.orchestra.portale.external.services.manager.CiRoService.java

@Override
public String getResponse(Map<String, String[]> mapParams) {
    try {/*from  w ww.j  av  a  2 s  . co  m*/
        HttpURLConnection urlConnection = (HttpURLConnection) new URL(getUrl).openConnection();
        urlConnection.setConnectTimeout(15000);
        urlConnection.setReadTimeout(30000);
        urlConnection.addRequestProperty("Accept-Language", Locale.getDefault().toString().replace('_', '-'));
        String result = IOUtils.toString(urlConnection.getInputStream());
        urlConnection.disconnect();
        return result;
    } catch (IOException e) {
        return "response{code:1,error:" + e.getMessage() + "}";
    }
}

From source file:com.example.ronald.tracle.RegistrationIntentService.java

private String downloadContent(String myurl) throws IOException {
    InputStream is = null;//from  w w w. ja va  2 s . c o m
    int length = 500;

    try {
        URL url = new URL(myurl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(10000 /* milliseconds */);
        conn.setConnectTimeout(15000 /* milliseconds */);
        conn.setRequestMethod("GET");
        conn.setDoInput(true);
        conn.connect();
        int response = conn.getResponseCode();
        Log.d(TAG, "The response is: " + response);
        is = conn.getInputStream();

        // Convert the InputStream into a string
        String contentAsString = convertInputStreamToString(is, length);
        return contentAsString;
    } finally {
        if (is != null) {
            is.close();
        }
    }
}

From source file:com.mercandalli.android.apps.files.common.net.TaskGet.java

@Override
protected String doInBackground(Void... urls) {
    try {/*from   ww  w .  j av a  2 s . c  o m*/
        if (this.parameters != null) {
            if (!StringUtils.isNullOrEmpty(Config.getNotificationId())) {
                parameters.add(new StringPair("android_id", "" + Config.getNotificationId()));
            }
            url = NetUtils.addUrlParameters(url, parameters);
        }

        Log.d("TaskGet", "url = " + url);

        URL tmp_url = new URL(url);
        HttpURLConnection conn = (HttpURLConnection) tmp_url.openConnection();
        conn.setReadTimeout(10_000);
        conn.setConnectTimeout(15_000);
        conn.setRequestMethod("GET");
        if (isAuthentication) {
            conn.setRequestProperty("Authorization", "Basic " + Config.getUserToken());
        }
        conn.setUseCaches(false);
        conn.setDoInput(true);

        conn.connect(); // Starts the query
        int responseCode = conn.getResponseCode();
        InputStream inputStream = new BufferedInputStream(conn.getInputStream());

        // convert inputstream to string
        String resultString = convertInputStreamToString(inputStream);

        //int responseCode = response.getStatusLine().getStatusCode();
        if (responseCode >= 300) {
            resultString = "Status Code " + responseCode + ". " + resultString;
        }

        conn.disconnect();

        return resultString;
    } catch (IOException e) {
        Log.e(getClass().getName(), "IOException", e);
    }
    return null;
}

From source file:com.cloudbees.mtslaves.client.VirtualMachineRef.java

/**
 * Synchronously boot and wait until its fully booted.
 *
 * @return/* w w  w .ja  va2 s .  co m*/
 *      The latest virtual machine state reported by the server when we confirmed a boot.
 */
public VirtualMachine bootSync() throws IOException, InterruptedException {
    HttpURLConnection con = open("boot");
    con.setRequestMethod("POST");
    con.setReadTimeout(BOOT_SYNC_READ_TIMEOUT_MS);
    con.connect();
    drain(con);
    verifyResponseStatus(con);

    for (int i = 0; i < TIMEOUT; i++) {
        Thread.sleep(1000);
        VirtualMachine state = getState();

        if (state.state == null)
            throw new IllegalStateException("Virtual machine failed to boot: " + state.json);

        switch (state.state.getId()) {
        default:
            throw new IllegalStateException(state.state.getId().toString());
        case booting:
            continue; // wait some more
        case running:
            return state;
        case error:
            throw new IllegalStateException("Virtual machine failed to boot: " + state.json);
        }
    }

    throw new IOException("Time out: VM is taking forever to boot" + url);
}

From source file:com.orchestra.portale.external.services.manager.CiRoService.java

@Override
public String load() {
    try {/*from ww w . ja  va 2s . co  m*/
        deletePois();
        HttpURLConnection urlConnection = (HttpURLConnection) new URL(loadUrl).openConnection();
        urlConnection.setConnectTimeout(15000);
        urlConnection.setReadTimeout(30000);
        String result = IOUtils.toString(urlConnection.getInputStream());
        urlConnection.disconnect();
        JsonParser parser = new JsonParser();
        JsonElement json = parser.parse(result);
        JsonArray puntiCiro = json.getAsJsonObject().get("puntiCiro").getAsJsonArray();
        StringBuilder insertedPoi = new StringBuilder();
        for (int i = 0; i < puntiCiro.size(); i++) {
            insertedPoi.append(createPoi(i, puntiCiro));
        }
        return insertedPoi.toString();
    } catch (Exception e) {
        return "response{code:1,error:" + e.toString() + "}";
    }
}

From source file:com.notonthehighstreet.ratel.internal.utility.HttpRequest.java

public int call(final HttpURLConnection connection, final String method, final Map<String, String> headers,
        final Object body) throws IOException {

    connection.setConnectTimeout(TEN_SECONDS);
    connection.setReadTimeout(TEN_SECONDS);

    connection.setRequestMethod(method);

    for (final Map.Entry<String, String> header : headers.entrySet()) {
        connection.setRequestProperty(header.getKey(), header.getValue());
    }/*from w w w.  j  av  a2  s. c o m*/

    connection.setDoOutput(true);

    mapper.writeValue(connection.getOutputStream(), body);

    return connection.getResponseCode();

}

From source file:KV78Tester.java

public void checkLines(String[] lines) throws IOException {
    System.out.println("Checking " + lines.length + " lines");
    for (int i = 0; i < lines.length; i++) {
        String uri = "http://openov.nl:5078/line/" + lines[i];
        URL url = new URL(uri);
        HttpURLConnection uc = (HttpURLConnection) url.openConnection();
        uc.setRequestProperty("User-Agent", "KV78Turbo-Test");
        uc.setConnectTimeout(60000);/*ww w . j av  a  2 s  .  c o  m*/
        uc.setReadTimeout(60000);
        BufferedReader in;
        in = new BufferedReader(new InputStreamReader(uc.getInputStream(), "UTF-8"));
        try {
            checkLines(in);
        } finally {
            uc.disconnect();
        }
    }
}

From source file:com.nexmo.sdk.core.client.Client.java

/**
 * Prepare a new connection with necessary custom header fields.
 * @param request The request object.//from w w  w.ja v a 2s. c  o m
 *
 * @return A new url connection.
 * @throws IOException if an error occurs while opening the connection.
 */
public HttpURLConnection initConnection(Request request) throws IOException {
    // Generate signature using pre-shared key.
    RequestSigning.constructSignatureForRequestParameters(request.getParams(), request.getSecretKey());

    // Construct connection with necessary custom headers.
    URL url = constructUrlGetConnection(request.getParams(), request.getMethod(), request.getUrl());
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("GET");
    connection.setReadTimeout(Defaults.CONNECTION_READ_TIMEOUT);
    connection.setConnectTimeout(Defaults.CONNECTION_TIMEOUT);
    connection.setDoInput(true);
    connection.setDoOutput(true);
    connection.addRequestProperty(HTTP.CONTENT_ENCODING, Config.PARAMS_ENCODING);
    connection.addRequestProperty(BaseService.OS_FAMILY, Config.OS_ANDROID);
    connection.addRequestProperty(BaseService.OS_REVISION, DeviceProperties.getApiLevel());
    connection.addRequestProperty(BaseService.SDK_REVISION, Config.SDK_REVISION_CODE);

    return connection;
}

From source file:com.eTilbudsavis.etasdk.network.impl.HttpURLNetwork.java

private HttpURLConnection openConnection(URL url, Request<?> request) throws IOException {

    HttpURLConnection connection = (HttpURLConnection) url.openConnection();

    connection.setConnectTimeout(request.getTimeOut());
    connection.setReadTimeout(request.getTimeOut());
    connection.setUseCaches(false);/*  w w  w .  jav a  2s . c om*/
    connection.setDoInput(true);

    // use caller-provided custom SslSocketFactory, if any, for HTTPS
    //        if ("https".equals(url.getProtocol()) && mSslSocketFactory != null) {
    //            ((HttpsURLConnection)connection).setSSLSocketFactory(mSslSocketFactory);
    //        }

    return connection;
}

From source file:org.piraso.replacer.spring.remoting.PirasoHttpInvokerRequestExecutorWithTimeout.java

protected void prepareConnection(HttpURLConnection con, int contentLength) throws IOException {
    super.prepareConnection(con, contentLength);

    if (context.isMonitored()) {
        con.setConnectTimeout(getConnectTimeoutMs() * 5);
        con.setReadTimeout(getReadTimeoutMs() * 5);
    } else {/*from  w ww.  j  a  v  a2  s . c o  m*/
        con.setConnectTimeout(getConnectTimeoutMs());
        con.setReadTimeout(getReadTimeoutMs());
    }
}