Search in sources :

Example 26 with Builder

use of okhttp3.Request.Builder in project jianshi by wingjay.

the class AppModule method provideOkHttpClient.

@Provides
@Singleton
OkHttpClient provideOkHttpClient(GlobalRequestInterceptor globalRequestInterceptor) {
    OkHttpClient.Builder builder = new OkHttpClient.Builder().connectionPool(new ConnectionPool(5, 59, TimeUnit.SECONDS)).connectTimeout(20, TimeUnit.SECONDS).readTimeout(20, TimeUnit.SECONDS).addInterceptor(globalRequestInterceptor).retryOnConnectionFailure(false);
    if (BuildConfig.DEBUG) {
        builder.addNetworkInterceptor(new StethoInterceptor());
    }
    HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
    httpLoggingInterceptor.setLevel(BuildConfig.DEBUG ? HttpLoggingInterceptor.Level.BODY : HttpLoggingInterceptor.Level.NONE);
    builder.addInterceptor(httpLoggingInterceptor);
    return builder.build();
}
Also used : ConnectionPool(okhttp3.ConnectionPool) OkHttpClient(okhttp3.OkHttpClient) StethoInterceptor(com.facebook.stetho.okhttp3.StethoInterceptor) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) Singleton(javax.inject.Singleton) Provides(dagger.Provides)

Example 27 with Builder

use of okhttp3.Request.Builder in project BookReader by JustWayward.

the class BookApiModule method provideOkHttpClient.

@Provides
public OkHttpClient provideOkHttpClient() {
    LoggingInterceptor logging = new LoggingInterceptor(new Logger());
    logging.setLevel(LoggingInterceptor.Level.BODY);
    OkHttpClient.Builder builder = new OkHttpClient.Builder().connectTimeout(10, TimeUnit.SECONDS).connectTimeout(20 * 1000, TimeUnit.MILLISECONDS).readTimeout(20 * 1000, TimeUnit.MILLISECONDS).retryOnConnectionFailure(// 失败重发
    true).addInterceptor(new HeaderInterceptor()).addInterceptor(logging);
    return builder.build();
}
Also used : LoggingInterceptor(com.justwayward.reader.api.support.LoggingInterceptor) OkHttpClient(okhttp3.OkHttpClient) HeaderInterceptor(com.justwayward.reader.api.support.HeaderInterceptor) Logger(com.justwayward.reader.api.support.Logger) Provides(dagger.Provides)

Example 28 with Builder

use of okhttp3.Request.Builder in project spring-framework by spring-projects.

the class OkHttp3ClientHttpRequestFactory method buildRequest.

static Request buildRequest(HttpHeaders headers, byte[] content, URI uri, HttpMethod method) throws MalformedURLException {
    okhttp3.MediaType contentType = getContentType(headers);
    RequestBody body = (content.length > 0 || okhttp3.internal.http.HttpMethod.requiresRequestBody(method.name()) ? RequestBody.create(contentType, content) : null);
    Request.Builder builder = new Request.Builder().url(uri.toURL()).method(method.name(), body);
    for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
        String headerName = entry.getKey();
        for (String headerValue : entry.getValue()) {
            builder.addHeader(headerName, headerValue);
        }
    }
    return builder.build();
}
Also used : Request(okhttp3.Request) List(java.util.List) Map(java.util.Map) RequestBody(okhttp3.RequestBody)

Example 29 with Builder

use of okhttp3.Request.Builder in project muzei by romannurik.

the class OkHttpClientFactory method enableTls12.

/**
     * Enable TLS on the OKHttp builder by setting a custom SocketFactory
     */
private static OkHttpClient.Builder enableTls12(OkHttpClient.Builder client) {
    Log.i(TAG, "Enabling HTTPS compatibility mode");
    try {
        TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        trustManagerFactory.init((KeyStore) null);
        TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
        if (trustManagers.length != 1 || !(trustManagers[0] instanceof X509TrustManager)) {
            throw new IllegalStateException("Unexpected default trust managers:" + Arrays.toString(trustManagers));
        }
        X509TrustManager trustManager = (X509TrustManager) trustManagers[0];
        client.sslSocketFactory(new TLSSocketFactory(), trustManager);
        ConnectionSpec cs = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS).tlsVersions(TlsVersion.TLS_1_2, TlsVersion.TLS_1_1).build();
        List<ConnectionSpec> specs = new ArrayList<>();
        specs.add(cs);
        specs.add(ConnectionSpec.COMPATIBLE_TLS);
        specs.add(ConnectionSpec.CLEARTEXT);
        client.connectionSpecs(specs);
    } catch (Exception exc) {
        Log.e(TAG, "Error while setting TLS", exc);
    }
    return client;
}
Also used : ConnectionSpec(okhttp3.ConnectionSpec) X509TrustManager(javax.net.ssl.X509TrustManager) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) ArrayList(java.util.ArrayList) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager)

Example 30 with Builder

use of okhttp3.Request.Builder in project Fast-Android-Networking by amitshekhariitbhu.

the class RxInternalNetworking method generateDownloadObservable.

public static <T> Observable<T> generateDownloadObservable(final RxANRequest request) {
    Request okHttpRequest;
    Request.Builder builder = new Request.Builder().url(request.getUrl());
    InternalNetworking.addHeadersToRequestBuilder(builder, request);
    builder = builder.get();
    if (request.getCacheControl() != null) {
        builder.cacheControl(request.getCacheControl());
    }
    okHttpRequest = builder.build();
    OkHttpClient okHttpClient;
    if (request.getOkHttpClient() != null) {
        okHttpClient = request.getOkHttpClient().newBuilder().cache(InternalNetworking.sHttpClient.cache()).addNetworkInterceptor(new Interceptor() {

            @Override
            public Response intercept(Chain chain) throws IOException {
                Response originalResponse = chain.proceed(chain.request());
                return originalResponse.newBuilder().body(new ResponseProgressBody(originalResponse.body(), request.getDownloadProgressListener())).build();
            }
        }).build();
    } else {
        okHttpClient = InternalNetworking.sHttpClient.newBuilder().addNetworkInterceptor(new Interceptor() {

            @Override
            public Response intercept(Chain chain) throws IOException {
                Response originalResponse = chain.proceed(chain.request());
                return originalResponse.newBuilder().body(new ResponseProgressBody(originalResponse.body(), request.getDownloadProgressListener())).build();
            }
        }).build();
    }
    request.setCall(okHttpClient.newCall(okHttpRequest));
    return Observable.create(new ANOnSubscribe<T>(request));
}
Also used : Response(okhttp3.Response) ANResponse(com.androidnetworking.common.ANResponse) OkHttpClient(okhttp3.OkHttpClient) GET(com.androidnetworking.common.Method.GET) PUT(com.androidnetworking.common.Method.PUT) POST(com.androidnetworking.common.Method.POST) Request(okhttp3.Request) IOException(java.io.IOException) ResponseProgressBody(com.androidnetworking.internal.ResponseProgressBody) Interceptor(okhttp3.Interceptor)

Aggregations

Request (okhttp3.Request)163 Response (okhttp3.Response)120 OkHttpClient (okhttp3.OkHttpClient)91 IOException (java.io.IOException)83 RequestBody (okhttp3.RequestBody)67 Test (org.junit.Test)67 MultipartBody (okhttp3.MultipartBody)35 File (java.io.File)31 Map (java.util.Map)31 MockResponse (okhttp3.mockwebserver.MockResponse)31 HttpUrl (okhttp3.HttpUrl)29 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)27 Call (okhttp3.Call)25 Interceptor (okhttp3.Interceptor)24 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)22 Retrofit (retrofit2.Retrofit)20 Builder (okhttp3.Request.Builder)19 ResponseBody (okhttp3.ResponseBody)18 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)17 Builder (okhttp3.OkHttpClient.Builder)17