use of okhttp3.FormBody.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.FormBody.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.FormBody.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;
}
}
use of okhttp3.FormBody.Builder in project FlareBot by FlareBot.
the class WebUtils method post.
public static Response post(Request.Builder builder) throws IOException {
Response res = client.newCall(builder.build()).execute();
ResponseBody body = res.body();
if (res.code() >= 200 && res.code() < 300)
return res;
else
throw new IllegalStateException("Failed to POST to '" + builder.build().url() + "'! Code: " + res.code() + ", Message: " + res.message() + ", Body: " + (body != null ? body.string().replace("\n", "").replace("\t", " ").replaceAll(" +", " ") : "null"));
}
use of okhttp3.FormBody.Builder in project SeeWeather by xcc3641.
the class RetrofitSingleton method initOkHttp.
private static void initOkHttp() {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
// 缓存 http://www.jianshu.com/p/93153b34310e
File cacheFile = new File(C.NET_CACHE);
Cache cache = new Cache(cacheFile, 1024 * 1024 * 50);
Interceptor cacheInterceptor = chain -> {
Request request = chain.request();
if (!Util.isNetworkConnected(BaseApplication.getAppContext())) {
request = request.newBuilder().cacheControl(CacheControl.FORCE_CACHE).build();
}
Response response = chain.proceed(request);
Response.Builder newBuilder = response.newBuilder();
if (Util.isNetworkConnected(BaseApplication.getAppContext())) {
int maxAge = 0;
// 有网络时 设置缓存超时时间0个小时
newBuilder.header("Cache-Control", "public, max-age=" + maxAge);
} else {
// 无网络时,设置超时为4周
int maxStale = 60 * 60 * 24 * 28;
newBuilder.header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale);
}
return newBuilder.build();
};
builder.cache(cache).addInterceptor(cacheInterceptor);
if (BuildConfig.DEBUG) {
builder.addNetworkInterceptor(new StethoInterceptor());
}
// 设置超时
builder.connectTimeout(15, TimeUnit.SECONDS);
builder.readTimeout(20, TimeUnit.SECONDS);
builder.writeTimeout(20, TimeUnit.SECONDS);
// 错误重连
builder.retryOnConnectionFailure(true);
sOkHttpClient = builder.build();
}
Aggregations