Search in sources :

Example 91 with Builder

use of okhttp3.OkHttpClient.Builder in project BBS-Android by bdpqchen.

the class UpdatePasswordClient method getUnSaveBuilder.

private static OkHttpClient.Builder getUnSaveBuilder() {
    try {
        // Create a trust manager that does not validate certificate chains
        final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

            @Override
            public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) {
            }

            @Override
            public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) {
            }

            @Override
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return new java.security.cert.X509Certificate[] {};
            }
        } };
        // Install the all-trusting trust manager
        final SSLContext sslContext = SSLContext.getInstance("SSL");
        sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
        // Create an ssl socket factory with our all-trusting manager
        final javax.net.ssl.SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.sslSocketFactory(sslSocketFactory);
        builder.hostnameVerifier(new HostnameVerifier() {

            @Override
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        });
        return builder;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : OkHttpClient(okhttp3.OkHttpClient) SSLSession(javax.net.ssl.SSLSession) SSLContext(javax.net.ssl.SSLContext) X509TrustManager(javax.net.ssl.X509TrustManager) TrustManager(javax.net.ssl.TrustManager) HostnameVerifier(javax.net.ssl.HostnameVerifier) X509TrustManager(javax.net.ssl.X509TrustManager)

Example 92 with Builder

use of okhttp3.OkHttpClient.Builder in project BBS-Android by bdpqchen.

the class RxDoHttpClient method doUpdateAvatar.

public Observable<BaseResponse<BaseModel>> doUpdateAvatar(File file) {
    if (file != null) {
        //表单类型
        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        RequestBody imageBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        //imgfile 后台接收图片流的参数名
        builder.addFormDataPart("cropped", file.getName(), imageBody);
        List<MultipartBody.Part> parts = builder.build().parts();
        return mApi.doUpdateAvatar(getLatestAuthentication(), parts);
    } else {
        return null;
    }
}
Also used : MultipartBody(okhttp3.MultipartBody) RequestBody(okhttp3.RequestBody)

Example 93 with Builder

use of okhttp3.OkHttpClient.Builder in project WeexErosFramework by bmfe.

the class AxiosManager method createClient.

public OkHttpClient createClient(Context context, long timeout) {
    CookieJarImpl cookieJar = new CookieJarImpl(new BMPersistentCookieStore(context));
    OkHttpClient.Builder builder = new OkHttpClient.Builder().addInterceptor(new LoggerInterceptor("TAG")).connectTimeout(timeout == 0 ? 3000L : timeout, TimeUnit.MILLISECONDS).readTimeout(timeout == 0 ? 30000L : timeout, TimeUnit.MILLISECONDS).cookieJar(cookieJar);
    if (DebugableUtil.isDebug()) {
        builder.addNetworkInterceptor(new WeexOkhttp3Interceptor());
    }
    return builder.build();
}
Also used : BMPersistentCookieStore(com.benmu.framework.http.BMPersistentCookieStore) OkHttpClient(okhttp3.OkHttpClient) LoggerInterceptor(com.benmu.framework.http.okhttp.log.LoggerInterceptor) WeexOkhttp3Interceptor(com.benmu.framework.extend.adapter.WeexOkhttp3Interceptor) CookieJarImpl(com.benmu.framework.http.okhttp.cookie.CookieJarImpl) OkHttpRequestBuilder(com.benmu.framework.http.okhttp.builder.OkHttpRequestBuilder) OtherRequestBuilder(com.benmu.framework.http.okhttp.builder.OtherRequestBuilder) GetBuilder(com.benmu.framework.http.okhttp.builder.GetBuilder) PostFormBuilder(com.benmu.framework.http.okhttp.builder.PostFormBuilder)

Example 94 with Builder

use of okhttp3.OkHttpClient.Builder in project openremote by openremote.

the class TokenService method getUnsafeOkHttpClient.

private static OkHttpClient getUnsafeOkHttpClient() {
    try {
        // Create a trust manager that does not validate certificate chains
        final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

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

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

            @Override
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return new java.security.cert.X509Certificate[] {};
            }
        } };
        // Install the all-trusting trust manager
        final SSLContext sslContext = SSLContext.getInstance("SSL");
        sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
        // Create an ssl socket factory with our all-trusting manager
        final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.sslSocketFactory(sslSocketFactory);
        builder.hostnameVerifier(new HostnameVerifier() {

            @Override
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        });
        OkHttpClient okHttpClient = builder.build();
        return okHttpClient;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : OkHttpClient(okhttp3.OkHttpClient) SSLSession(javax.net.ssl.SSLSession) SSLContext(javax.net.ssl.SSLContext) 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)

Example 95 with Builder

use of okhttp3.OkHttpClient.Builder in project DevRing by LJYcoder.

the class RingModule method okHttpClient.

@Singleton
@Provides
OkHttpClient okHttpClient(Application application, OkHttpClient.Builder builder, HttpConfig httpConfig, HttpProgressInterceptor progressInterceptor) {
    if (httpConfig.getConnectTimeout() > 0) {
        builder.connectTimeout(httpConfig.getConnectTimeout(), TimeUnit.SECONDS);
    }
    if (httpConfig.isUseLog()) {
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        builder.addInterceptor(loggingInterceptor);
    }
    if (httpConfig.isUseCache()) {
        HttpCacheInterceptor cacheInterceptor = new HttpCacheInterceptor(application, httpConfig.getCacheTimeWithNet(), httpConfig.getCacheTimeWithoutNet());
        // 缓存目录
        File cacheFile;
        if (httpConfig.getCacheFolder() != null && httpConfig.getCacheFolder().isDirectory()) {
            cacheFile = httpConfig.getCacheFolder();
        } else {
            cacheFile = FileUtil.getDirectory(FileUtil.getExternalCacheDir(application), "retrofit_http_cache");
        }
        // 大小默认20Mb
        Cache cache = new Cache(cacheFile, httpConfig.getCacheSize() > 0 ? httpConfig.getCacheSize() : 1024 * 1024 * 20);
        builder.addInterceptor(cacheInterceptor);
        builder.addNetworkInterceptor(cacheInterceptor);
        builder.cache(cache);
    }
    if (!CollectionUtil.isEmpty(httpConfig.getMapHeader())) {
        HttpHeaderInterceptor headerInterceptor = new HttpHeaderInterceptor(httpConfig.getMapHeader());
        builder.addInterceptor(headerInterceptor);
    }
    builder.addNetworkInterceptor(progressInterceptor);
    return builder.build();
}
Also used : HttpHeaderInterceptor(com.ljy.devring.http.support.interceptor.HttpHeaderInterceptor) HttpCacheInterceptor(com.ljy.devring.http.support.interceptor.HttpCacheInterceptor) File(java.io.File) HttpLoggingInterceptor(com.ljy.devring.http.support.interceptor.HttpLoggingInterceptor) Cache(okhttp3.Cache) DiskCache(com.ljy.devring.cache.support.DiskCache) SpCache(com.ljy.devring.cache.support.SpCache) MemoryCache(com.ljy.devring.cache.support.MemoryCache) Singleton(javax.inject.Singleton) Provides(dagger.Provides)

Aggregations

Request (okhttp3.Request)47 Response (okhttp3.Response)44 OkHttpClient (okhttp3.OkHttpClient)31 IOException (java.io.IOException)28 RequestBody (okhttp3.RequestBody)27 Test (org.junit.Test)17 Interceptor (okhttp3.Interceptor)11 File (java.io.File)9 Provides (dagger.Provides)8 X509TrustManager (javax.net.ssl.X509TrustManager)8 Retrofit (retrofit2.Retrofit)8 URI (java.net.URI)7 Map (java.util.Map)7 MultipartBody (okhttp3.MultipartBody)7 Singleton (javax.inject.Singleton)6 HttpUrl (okhttp3.HttpUrl)6 ResponseBody (okhttp3.ResponseBody)6 SSLContext (javax.net.ssl.SSLContext)5 TrustManager (javax.net.ssl.TrustManager)5 TestClients.clientRequest (keywhiz.TestClients.clientRequest)5