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();
}
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();
}
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);
}
}
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();
}
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;
}
}
Aggregations