Example usage for com.squareup.okhttp OkHttpClient setConnectTimeout

List of usage examples for com.squareup.okhttp OkHttpClient setConnectTimeout

Introduction

In this page you can find the example usage for com.squareup.okhttp OkHttpClient setConnectTimeout.

Prototype

public void setConnectTimeout(long timeout, TimeUnit unit) 

Source Link

Document

Sets the default connect timeout for new connections.

Usage

From source file:org.sonar.runner.impl.OkHttpClientFactory.java

License:Open Source License

static OkHttpClient create(JavaVersion javaVersion) {
    OkHttpClient httpClient = new OkHttpClient();
    httpClient.setConnectTimeout(CONNECT_TIMEOUT_MILLISECONDS, TimeUnit.MILLISECONDS);
    httpClient.setReadTimeout(READ_TIMEOUT_MILLISECONDS, TimeUnit.MILLISECONDS);
    ConnectionSpec tls = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS).allEnabledTlsVersions()
            .allEnabledCipherSuites().supportsTlsExtensions(true).build();
    httpClient.setConnectionSpecs(asList(tls, ConnectionSpec.CLEARTEXT));
    if (javaVersion.isJava7()) {
        // OkHttp executes SSLContext.getInstance("TLS") by default (see
        // https://github.com/square/okhttp/blob/c358656/okhttp/src/main/java/com/squareup/okhttp/OkHttpClient.java#L616)
        // As only TLS 1.0 is enabled by default in Java 7, the SSLContextFactory must be changed
        // in order to support all versions from 1.0 to 1.2.
        // Note that this is not overridden for Java 8 as TLS 1.2 is enabled by default.
        // Keeping getInstance("TLS") allows to support potential future versions of TLS on Java 8.
        try {/*from  ww w  .j a v  a 2s.  co  m*/
            httpClient.setSslSocketFactory(
                    new Tls12Java7SocketFactory((SSLSocketFactory) SSLSocketFactory.getDefault()));
        } catch (Exception e) {
            throw new IllegalStateException("Fail to init TLS context", e);
        }
    }
    return httpClient;
}

From source file:org.sufficientlysecure.keychain.keyimport.HkpKeyserver.java

License:Apache License

/**
 * returns a client with pinned certificate if necessary
 *
 * @param url   url to be queried by client
 * @param proxy proxy to be used by client
 * @return client with a pinned certificate if necessary
 *//*from  w  w  w  . ja v  a 2  s . c om*/
public static OkHttpClient getClient(URL url, Proxy proxy) throws IOException {
    OkHttpClient client = new OkHttpClient();

    try {
        TlsHelper.usePinnedCertificateIfAvailable(client, url);
    } catch (TlsHelper.TlsHelperException e) {
        Log.w(Constants.TAG, e);
    }

    // don't follow any redirects
    client.setFollowRedirects(false);
    client.setFollowSslRedirects(false);

    if (proxy != null) {
        client.setProxy(proxy);
        client.setConnectTimeout(30000, TimeUnit.MILLISECONDS);
    } else {
        client.setProxy(Proxy.NO_PROXY);
        client.setConnectTimeout(5000, TimeUnit.MILLISECONDS);
    }
    client.setReadTimeout(45000, TimeUnit.MILLISECONDS);

    return client;
}

From source file:org.wso2.carbon.identity.authenticator.duo.DuoHttp.java

License:Open Source License

public Response executeHttpRequest() throws Exception {
    String url = "https://" + host + uri;
    String queryString = createQueryString();
    Request.Builder builder = new Request.Builder();
    if (method.equals("POST")) {
        builder.post(RequestBody.create(FORM_ENCODED, queryString));
    } else if (method.equals("PUT")) {
        builder.put(RequestBody.create(FORM_ENCODED, queryString));
    } else if (method.equals("GET")) {
        if (queryString.length() > 0) {
            url += "?" + queryString;
        }//  w  w  w . jav  a  2 s. com
        builder.get();
    } else if (method.equals("DELETE")) {
        if (queryString.length() > 0) {
            url += "?" + queryString;
        }
        builder.delete();
    } else {
        throw new UnsupportedOperationException("Unsupported method: " + method);
    }
    Request request = builder.url(url).build();
    // Set up client.
    OkHttpClient httpclient = new OkHttpClient();
    if (proxy != null) {
        httpclient.setProxy(proxy);
    }
    httpclient.setConnectTimeout(timeout, TimeUnit.SECONDS);
    httpclient.setWriteTimeout(timeout, TimeUnit.SECONDS);
    httpclient.setReadTimeout(timeout, TimeUnit.SECONDS);
    // finish and execute request
    builder.headers(headers.build());
    return httpclient.newCall(builder.build()).execute();
}

From source file:ph.devcon.android.base.module.APIModule.java

License:Apache License

@Provides
public RestAdapter provideRestAdapter() {
    int SIZE_OF_CACHE = 1024;
    OkHttpClient ok = new OkHttpClient();
    ok.setReadTimeout(30, TimeUnit.SECONDS);
    ok.setConnectTimeout(30, TimeUnit.SECONDS);
    try {//  w w  w  . j a va 2 s. c o  m
        Cache responseCache = new Cache(DevConApplication.getInstance().getCacheDir(), SIZE_OF_CACHE);
        ok.setCache(responseCache);
    } catch (Exception e) {
        Log.d("OkHttp", "Unable to set http cache", e);
    }
    Executor executor = Executors.newCachedThreadPool();
    return new RestAdapter.Builder().setExecutors(executor, executor).setClient(new OkClient(ok))
            .setEndpoint(DevConApplication.API_ENDPOINT).setRequestInterceptor(new ApiRequestInterceptor())
            //                .setLogLevel(RestAdapter.LogLevel.FULL).setLog(new AndroidLog("RETROFIT"))
            .build();
}

From source file:retrofit.client.OkClient.java

License:Apache License

private static OkHttpClient generateDefaultOkHttp() {
    OkHttpClient client = new OkHttpClient();
    client.setConnectTimeout(Defaults.CONNECT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    client.setReadTimeout(Defaults.READ_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    return client;
}

From source file:retrofit.Platform.java

License:Apache License

OkHttpClient defaultClient() {
    OkHttpClient client = new OkHttpClient();
    client.setConnectTimeout(15, TimeUnit.SECONDS);
    client.setReadTimeout(15, TimeUnit.SECONDS);
    client.setWriteTimeout(15, TimeUnit.SECONDS);
    return client;
}

From source file:stash.samples.hockeyloader.network.client.HttpClient.java

License:Apache License

private static OkHttpClient createDefault() {
    OkHttpClient client = new OkHttpClient();
    client.setConnectTimeout(DEFAULT_CONNECT_TIMEOUT_SECONDS, TimeUnit.SECONDS);
    client.setReadTimeout(DEFAULT_READ_TIMEOUT_SECONDS, TimeUnit.SECONDS);
    return client;
}

From source file:syncthing.api.SyncthingApiLongpollModule.java

License:Open Source License

@Provides
@SessionScope//  w w  w  . j  av  a2 s . co m
@Named("longpoll")
public SyncthingApi provideSyncthingApi(Gson gson, OkHttpClient okClient, SyncthingApiConfig config) {
    OkHttpClient client = okClient.clone();
    client.setConnectTimeout(LONGPOLL_CONNECT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    client.setReadTimeout(LONGPOLL_READ_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    client.setSslSocketFactory(SyncthingSSLSocketFactory.create(config.getCACert()));
    client.setHostnameVerifier(new NullHostNameVerifier());
    client.interceptors().add(new SyncthingApiInterceptor(config));
    Retrofit.Builder b = new Retrofit.Builder().addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create(gson)).client(client).baseUrl(config.getBaseUrl());
    if (BuildConfig.DEBUG) {
        b.validateEagerly();
    }
    return b.build().create(SyncthingApi.class);
}

From source file:syncthing.api.SyncthingApiModule.java

License:Open Source License

@Provides
@SessionScope/*from  w ww  . j  a v a2  s .c  o  m*/
public SyncthingApi provideSyncthingApi(Gson gson, OkHttpClient okClient, SyncthingApiConfig config) {
    OkHttpClient client = okClient.clone();
    client.setConnectTimeout(CONNECT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    client.setReadTimeout(READ_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    client.setSslSocketFactory(SyncthingSSLSocketFactory.create(config.getCACert()));
    client.setHostnameVerifier(new NullHostNameVerifier());
    client.interceptors().add(new SyncthingApiInterceptor(config));
    Retrofit.Builder b = new Retrofit.Builder().addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create(gson)).client(client).baseUrl(config.getBaseUrl());
    if (BuildConfig.DEBUG) {
        b.validateEagerly();
    }
    return b.build().create(SyncthingApi.class);
}

From source file:zblibrary.demo.manager.HttpRequest.java

License:Apache License

/**
 * @param url//from   www  . j a v  a2s. c  o  m
 * @return
 */
private OkHttpClient getHttpClient(String url) {
    Log.i(TAG, "getHttpClient  url = " + url);
    if (StringUtil.isNotEmpty(url, true) == false) {
        Log.e(TAG, "getHttpClient  StringUtil.isNotEmpty(url, true) == false >> return null;");
        return null;
    }

    OkHttpClient client = new OkHttpClient();
    client.setCookieHandler(new HttpHead());
    client.setConnectTimeout(15, TimeUnit.SECONDS);
    client.setWriteTimeout(10, TimeUnit.SECONDS);
    client.setReadTimeout(10, TimeUnit.SECONDS);
    //https?,??,???
    if (url.startsWith(StringUtil.URL_PREFIXs) && socketFactory != null) {
        client.setSslSocketFactory(socketFactory);
    }

    return client;
}