Example usage for java.net HttpURLConnection setConnectTimeout

List of usage examples for java.net HttpURLConnection setConnectTimeout

Introduction

In this page you can find the example usage for java.net HttpURLConnection 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:cn.garymb.wechatmoments.common.OkHttpStack.java

private HttpURLConnection openOkHttpURLConnection(OkUrlFactory factory, URL url, Request<?> request)
        throws IOException {
    HttpURLConnection connection = factory.open(url);
    int timeoutMs = request.getTimeoutMs();
    connection.setConnectTimeout(timeoutMs);
    connection.setReadTimeout(timeoutMs);
    connection.setUseCaches(false);/*from www . j  a v a 2s.c o m*/
    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:com.baasbox.android.HttpUrlConnectionClient.java

private HttpURLConnection openConnection(String urlString) throws BaasIOException, IOException {
    URL url = null;//www  .j  a  va2 s  .  c o m
    try {
        url = new URL(urlString);
    } catch (MalformedURLException e) {
        throw new BaasIOException("Error while parsing url " + urlString, e);
    }
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setConnectTimeout(config.httpConnectionTimeout);
    connection.setReadTimeout(config.httpSocketTimeout);
    connection.setInstanceFollowRedirects(true);
    connection.setDoInput(true);

    return connection;
}

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

protected HttpURLConnection open(String relUrl) throws IOException {
    URL u = getEntityUrl(relUrl);
    HttpURLConnection con = (HttpURLConnection) u.openConnection();
    con.addRequestProperty("Accept", "application/json");
    this.token.authorizeRequest(con);
    con.setConnectTimeout(DEFAULT_CONNECT_TIMEOUT_MS);
    con.setReadTimeout(DEFAULT_READ_TIMEOUT_MS);

    return con;/*from   ww w .  j a v a2  s .co m*/
}

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

/**
 * Prepare a new connection with necessary custom header fields.
 * @param request The request object./*www.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.lark.http.LarkHurlStack.java

/**
* Opens an {@link HttpURLConnection} with parameters.
* @param url//w  w w . j  a  v  a 2s .  co  m
* @return an open connection
* @throws IOException
*/
private HttpURLConnection openConnection(URL url, Request<?> request) throws IOException {
    HttpURLConnection connection = createConnection(url);

    int timeoutMs = request.getTimeoutMs();
    connection.setConnectTimeout(timeoutMs);
    connection.setReadTimeout(timeoutMs);
    connection.setUseCaches(false);
    connection.setDoInput(true);

    // use caller-provided custom SslSocketFactory, if any, for HTTPS
    if ("https".equals(url.getProtocol())) {
        HttpsTrustManager.allowAllSSL();//TODO really support?
    }

    return connection;
}

From source file:com.lovebridge.library.volley.toolbox.HurlStack.java

/**
 * Opens an {@link HttpURLConnection} with parameters.
 *
 * @param url/*from   w  w w.j  a  va  2 s  . com*/
 * @return an open connection
 * @throws IOException
 */
private HttpURLConnection openConnection(URL url, Request<?> request) throws IOException {
    HttpURLConnection connection = createConnection(url);
    int timeoutMs = request.getTimeoutMs();
    connection.setConnectTimeout(timeoutMs);
    connection.setReadTimeout(timeoutMs);
    connection.setUseCaches(false);
    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:com.nostra13.universalimageloader.core.download.BaseImageDownloader.java

/**
 * Create {@linkplain HttpURLConnection HTTP connection} for incoming URL
 *
 * @param url   URL to connect to//  w w w . j a v a2  s  . c o  m
 * @param extra Auxiliary object which was passed to {@link DisplayImageOptions.Builder#extraForDownloader(Object)
 *              DisplayImageOptions.extraForDownloader(Object)}; can be null
 * @return {@linkplain HttpURLConnection Connection} for incoming URL. Connection isn't established so it still configurable.
 * @throws IOException if some I/O error occurs during network request or if no InputStream could be created for
 *                     URL.
 */
protected HttpURLConnection createConnection(String url, Object extra) throws IOException {
    String encodedUrl = Uri.encode(url, ALLOWED_URI_CHARS);
    HttpURLConnection conn = (HttpURLConnection) new URL(encodedUrl).openConnection();
    conn.setConnectTimeout(connectTimeout);
    conn.setReadTimeout(readTimeout);
    return conn;

}

From source file:com.android.volley.util.HurlStack.java

/**
 * Opens an {@link HttpURLConnection} with parameters.
 * @param url//  w w w  .  j a v a  2  s.c o m
 * @return an open connection
 * @throws IOException
 */
private HttpURLConnection openConnection(URL url, Request<?> request) throws IOException {
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();

    int timeoutMs = request.getTimeoutMs();
    connection.setConnectTimeout(timeoutMs);
    connection.setReadTimeout(timeoutMs);
    connection.setUseCaches(false);
    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:com.example.socketmobile.android.warrantychecker.network.WarrantyCheck.java

private Object fetchWarranty(String id, String authString, String query) throws IOException {
    InputStream is = null;//  ww  w.  j  av  a2  s .  c  o m
    RegistrationApiResponse result = null;
    RegistrationApiErrorResponse errorResult = null;
    String authHeader = "Basic " + Base64.encodeToString(authString.getBytes(), Base64.NO_WRAP);

    try {
        URL url = new URL(baseUrl + id + "?" + query);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(10000 /* milliseconds */);
        conn.setConnectTimeout(10000 /* milliseconds */);
        //conn.setRequestMethod("GET");
        conn.setRequestProperty("Accept", "application/json");
        conn.setRequestProperty("Authorization", authHeader);
        conn.setDoInput(true);
        conn.setDoOutput(false);

        conn.connect();
        int response = conn.getResponseCode();
        Log.d(TAG, "WarrantyCheck query responded: " + response);
        switch (response / 100) {
        case 2:
            is = conn.getInputStream();
            RegistrationApiResponse.Reader reader = new RegistrationApiResponse.Reader();
            result = reader.readJsonStream(is);
            break;
        case 4:
        case 5:
            is = conn.getErrorStream();
            RegistrationApiErrorResponse.Reader errorReader = new RegistrationApiErrorResponse.Reader();
            errorResult = errorReader.readErrorJsonStream(is);
            break;
        }

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        return null;
    } finally {
        if (is != null) {
            is.close();
        }
    }
    return (result != null) ? result : errorResult;
}

From source file:id.nci.stm_9.HkpKeyServer.java

private String query(String request) throws QueryException, HttpError {
    InetAddress ips[];/*  w  ww .ja va2s  .  co m*/
    try {
        ips = InetAddress.getAllByName(mHost);
    } catch (UnknownHostException e) {
        throw new QueryException(e.toString());
    }
    for (int i = 0; i < ips.length; ++i) {
        try {
            String url = "http://" + ips[i].getHostAddress() + ":" + mPort + request;
            URL realUrl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
            conn.setConnectTimeout(5000);
            conn.setReadTimeout(25000);
            conn.connect();
            int response = conn.getResponseCode();
            if (response >= 200 && response < 300) {
                return readAll(conn.getInputStream(), conn.getContentEncoding());
            } else {
                String data = readAll(conn.getErrorStream(), conn.getContentEncoding());
                throw new HttpError(response, data);
            }
        } catch (MalformedURLException e) {
            // nothing to do, try next IP
        } catch (IOException e) {
            // nothing to do, try next IP
        }
    }

    throw new QueryException("querying server(s) for '" + mHost + "' failed");
}