use of okhttp3.OkHttpClient.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);
}
}
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);
}
}
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;
}
}
Aggregations