Search in sources :

Example 46 with OkHttpClient

use of okhttp3.OkHttpClient in project okhttp by square.

the class CallTest method asyncCallEngineInitialized.

/** https://github.com/square/okhttp/issues/1801 */
@Test
public void asyncCallEngineInitialized() throws Exception {
    OkHttpClient c = defaultClient().newBuilder().addInterceptor(new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            throw new IOException();
        }
    }).build();
    Request request = new Request.Builder().url(server.url("/")).build();
    c.newCall(request).enqueue(callback);
    RecordedResponse response = callback.await(request.url());
    assertEquals(request, response.request);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) Test(org.junit.Test)

Example 47 with OkHttpClient

use of okhttp3.OkHttpClient in project okhttp by square.

the class ClientAuthTest method missingClientAuthFailsForNeeds.

@Test
public void missingClientAuthFailsForNeeds() throws Exception {
    OkHttpClient client = buildClient(null, clientIntermediateCa);
    SSLSocketFactory socketFactory = buildServerSslSocketFactory(ClientAuth.NEEDS);
    server.useHttps(socketFactory, false);
    Call call = client.newCall(new Request.Builder().url(server.url("/")).build());
    try {
        call.execute();
        fail();
    } catch (SSLHandshakeException expected) {
    } catch (SocketException expected) {
    // JDK 9
    }
}
Also used : Call(okhttp3.Call) SocketException(java.net.SocketException) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) DelegatingSSLSocketFactory(okhttp3.DelegatingSSLSocketFactory) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) Test(org.junit.Test)

Example 48 with OkHttpClient

use of okhttp3.OkHttpClient in project okhttp by square.

the class OkUrlFactory method open.

HttpURLConnection open(URL url, Proxy proxy) {
    String protocol = url.getProtocol();
    OkHttpClient copy = client.newBuilder().proxy(proxy).build();
    if (protocol.equals("http"))
        return new OkHttpURLConnection(url, copy, urlFilter);
    if (protocol.equals("https"))
        return new OkHttpsURLConnection(url, copy, urlFilter);
    throw new IllegalArgumentException("Unexpected protocol: " + protocol);
}
Also used : OkHttpsURLConnection(okhttp3.internal.huc.OkHttpsURLConnection) OkHttpURLConnection(okhttp3.internal.huc.OkHttpURLConnection)

Example 49 with OkHttpClient

use of okhttp3.OkHttpClient in project CloudReader by youlookwhat.

the class HttpUtils method getOkClient.

public OkHttpClient getOkClient() {
    OkHttpClient client1;
    client1 = getUnsafeOkHttpClient();
    return client1;
}
Also used : OkHttpClient(okhttp3.OkHttpClient)

Example 50 with OkHttpClient

use of okhttp3.OkHttpClient in project CloudReader by youlookwhat.

the class HttpUtils method getUnsafeOkHttpClient.

public OkHttpClient getUnsafeOkHttpClient() {
    try {
        final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

            @Override
            public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            }

            @Override
            public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            }

            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[] {};
            }
        } };
        // Install the all-trusting trust manager
        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, trustAllCerts, new SecureRandom());
        // Create an ssl socket factory with our all-trusting manager
        SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
        OkHttpClient.Builder okBuilder = new OkHttpClient.Builder();
        okBuilder.readTimeout(20, TimeUnit.SECONDS);
        okBuilder.connectTimeout(10, TimeUnit.SECONDS);
        okBuilder.writeTimeout(20, TimeUnit.SECONDS);
        okBuilder.addInterceptor(new HttpHeadInterceptor());
        okBuilder.addInterceptor(getInterceptor());
        okBuilder.sslSocketFactory(sslSocketFactory);
        okBuilder.hostnameVerifier(new HostnameVerifier() {

            @Override
            public boolean verify(String hostname, SSLSession session) {
                //                    Log.d("HttpUtils", "==come");
                return true;
            }
        });
        return okBuilder.build();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : OkHttpClient(okhttp3.OkHttpClient) GsonBuilder(com.google.gson.GsonBuilder) SSLSession(javax.net.ssl.SSLSession) SecureRandom(java.security.SecureRandom) SSLContext(javax.net.ssl.SSLContext) X509Certificate(java.security.cert.X509Certificate) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager) HostnameVerifier(javax.net.ssl.HostnameVerifier) X509TrustManager(javax.net.ssl.X509TrustManager) SSLSocketFactory(javax.net.ssl.SSLSocketFactory)

Aggregations

OkHttpClient (okhttp3.OkHttpClient)149 Request (okhttp3.Request)73 Response (okhttp3.Response)61 IOException (java.io.IOException)52 Test (org.junit.Test)35 Call (okhttp3.Call)24 Retrofit (retrofit2.Retrofit)24 Interceptor (okhttp3.Interceptor)19 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)18 MockResponse (okhttp3.mockwebserver.MockResponse)18 File (java.io.File)15 GsonBuilder (com.google.gson.GsonBuilder)12 Provides (dagger.Provides)12 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)12 Gson (com.google.gson.Gson)10 ArrayList (java.util.ArrayList)10 List (java.util.List)10 Cache (okhttp3.Cache)10 Observable (rx.Observable)10 Singleton (javax.inject.Singleton)9