Search in sources :

Example 26 with Builder

use of okhttp3.OkHttpClient.Builder in project couchbase-lite-android by couchbase.

the class CBLWebSocket method setupOkHttpClient.

// -------------------------------------------------------------------------
// private methods
// -------------------------------------------------------------------------
private OkHttpClient setupOkHttpClient() throws GeneralSecurityException {
    OkHttpClient.Builder builder = new OkHttpClient.Builder();
    // timeouts
    builder.connectTimeout(10, TimeUnit.SECONDS).readTimeout(30, TimeUnit.SECONDS).writeTimeout(30, TimeUnit.SECONDS);
    // redirection
    builder.followRedirects(true).followSslRedirects(true);
    // authenticator
    Authenticator authenticator = setupAuthenticator();
    if (authenticator != null)
        builder.authenticator(authenticator);
    // trusted certificate (pinned certificate)
    setupTrustedCertificate(builder);
    return builder.build();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) Authenticator(okhttp3.Authenticator)

Example 27 with Builder

use of okhttp3.OkHttpClient.Builder in project couchbase-lite-android by couchbase.

the class CBLWebSocket method newRequest.

private Request newRequest() {
    Request.Builder builder = new Request.Builder();
    // Sets the URL target of this request.
    builder.url(uri.toString());
    // Set/update the "Host" header:
    String host = uri.getHost();
    if (uri.getPort() != -1)
        host = String.format(Locale.ENGLISH, "%s:%d", host, uri.getPort());
    builder.header("Host", host);
    // Construct the HTTP request:
    if (options != null) {
        // Extra Headers
        Map<String, Object> extraHeaders = (Map<String, Object>) options.get(kC4ReplicatorOptionExtraHeaders);
        if (extraHeaders != null) {
            for (Map.Entry<String, Object> entry : extraHeaders.entrySet()) {
                builder.header(entry.getKey(), entry.getValue().toString());
            }
        }
        // Cookies:
        String cookieString = (String) options.get(kC4ReplicatorOptionCookies);
        if (cookieString != null)
            builder.addHeader("Cookie", cookieString);
    }
    // Configure WebSocket related headers:
    String protocols = (String) options.get(kC4SocketOptionWSProtocols);
    if (protocols != null) {
        builder.header("Sec-WebSocket-Protocol", protocols);
    }
    return builder.build();
}
Also used : Request(okhttp3.Request) ByteString(okio.ByteString) HashMap(java.util.HashMap) Map(java.util.Map)

Example 28 with Builder

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

the class CollectionClient 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) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager) HostnameVerifier(javax.net.ssl.HostnameVerifier) X509TrustManager(javax.net.ssl.X509TrustManager)

Example 29 with Builder

use of okhttp3.OkHttpClient.Builder in project MantaroBot by Mantaro.

the class PlayerCmds method applyBadge.

private void applyBadge(MessageChannel channel, Badge badge, User author, EmbedBuilder builder) {
    if (badge == null) {
        channel.sendMessage(builder.build()).queue();
        return;
    }
    Message message = new MessageBuilder().setEmbed(builder.setThumbnail("attachment://avatar.png").build()).build();
    byte[] bytes;
    try {
        String url = author.getEffectiveAvatarUrl();
        if (url.endsWith(".gif")) {
            url = url.substring(0, url.length() - 3) + "png";
        }
        Response res = client.newCall(new Request.Builder().url(url).addHeader("User-Agent", MantaroInfo.USER_AGENT).build()).execute();
        ResponseBody body = res.body();
        if (body == null)
            throw new IOException("body is null");
        bytes = body.bytes();
        res.close();
    } catch (IOException e) {
        throw new AssertionError("io error", e);
    }
    channel.sendFile(badge.apply(bytes), "avatar.png", message).queue();
}
Also used : Response(okhttp3.Response) MessageBuilder(net.dv8tion.jda.core.MessageBuilder) Request(okhttp3.Request) IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody)

Example 30 with Builder

use of okhttp3.OkHttpClient.Builder in project MantaroBot by Mantaro.

the class WeebAPIRequester method request.

private String request(String endpoint, String e) {
    try {
        StringBuilder builder = new StringBuilder(endpoint);
        if (e != null) {
            builder.append("?");
            builder.append(e);
        }
        Request r = new Request.Builder().url(API_BASE_URL + builder.toString()).addHeader("User-Agent", MantaroInfo.USER_AGENT).addHeader("Authorization", AUTH_HEADER).build();
        Response r1 = httpClient.newCall(r).execute();
        String response = r1.body().string();
        r1.close();
        return response;
    } catch (Exception ex) {
        log.error("Error getting image from weeb.sh", ex);
        return null;
    }
}
Also used : Response(okhttp3.Response) Request(okhttp3.Request)

Aggregations

Request (okhttp3.Request)186 Response (okhttp3.Response)128 OkHttpClient (okhttp3.OkHttpClient)116 IOException (java.io.IOException)97 RequestBody (okhttp3.RequestBody)76 Test (org.junit.Test)68 File (java.io.File)41 MultipartBody (okhttp3.MultipartBody)40 HttpUrl (okhttp3.HttpUrl)39 Map (java.util.Map)34 MockResponse (okhttp3.mockwebserver.MockResponse)32 Call (okhttp3.Call)28 Interceptor (okhttp3.Interceptor)28 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)26 Retrofit (retrofit2.Retrofit)26 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)25 Builder (okhttp3.OkHttpClient.Builder)23 FormBody (okhttp3.FormBody)20 ResponseBody (okhttp3.ResponseBody)20 Builder (okhttp3.Request.Builder)19