Search in sources :

Example 86 with Builder

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

the class RxDoHttpClient method getUnsafeBuilder.

// 由于https在连接的过程中会遇到
//javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
//由于无证书的连接是不可信的,在此,建立Okhttp3连接时,选择信任所有的证书。参照
//https://blog.ijustyce.win/post/retrofit2%E4%B9%8Bhttps.html
public static OkHttpClient.Builder getUnsafeBuilder() {
    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((hostname, session) -> true);
        return builder;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : OkHttpClient(okhttp3.OkHttpClient) SSLContext(javax.net.ssl.SSLContext) CertificateException(java.security.cert.CertificateException) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) X509TrustManager(javax.net.ssl.X509TrustManager)

Example 87 with Builder

use of okhttp3.FormBody.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 88 with Builder

use of okhttp3.FormBody.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)

Aggregations

Request (okhttp3.Request)42 Response (okhttp3.Response)40 RequestBody (okhttp3.RequestBody)27 OkHttpClient (okhttp3.OkHttpClient)26 IOException (java.io.IOException)25 Test (org.junit.Test)17 Interceptor (okhttp3.Interceptor)9 File (java.io.File)8 URI (java.net.URI)7 X509TrustManager (javax.net.ssl.X509TrustManager)7 MultipartBody (okhttp3.MultipartBody)7 Map (java.util.Map)6 HttpUrl (okhttp3.HttpUrl)6 Provides (dagger.Provides)5 TestClients.clientRequest (keywhiz.TestClients.clientRequest)5 MediaType (okhttp3.MediaType)5 Retrofit (retrofit2.Retrofit)5 SSLContext (javax.net.ssl.SSLContext)4 TrustManager (javax.net.ssl.TrustManager)4 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)4