Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tata8k/e3cb5686c084888893152f1793ddb293 to your computer and use it in GitHub Desktop.
Save tata8k/e3cb5686c084888893152f1793ddb293 to your computer and use it in GitHub Desktop.
OkHttp Accept Self-signed SSL Certificate
// package ... ;
/**
* Copyright (C) 2014 Square, Inc.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* <p>
* Original source: https://github.com/square/okhttp/blob/master/samples/guide/src/main/java/okhttp3/recipes/PostMultipart.java
* Modified by: ErlangParasu <erlangparasu@protonmail.com> 2017-01-17
*/
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.KeyManager;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import okhttp3.Call;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class NetworkUtilsAcceptSelfSignedSslCert {
final private String TAG = "Tag.NetworkUtilsAcceptSelfSignedSslCert";
final private String HOST = "192.168.43.176";
final private String BASE_URL = "https://" + HOST;
public String run() {
/**
* Does OkHttp support accepting self-signed SSL certs?
* answered May 24 '16 at 7:25 by: Gugelhupf
*
* https://stackoverflow.com/questions/23103174/does-okhttp-support-accepting-self-signed-ssl-certs/37406961#37406961
*/
/**
* Modified by: ErlangParasu (erlangparasu@protonmail.com) (2017-02-22)
*/
/* Trust All Certificates */
final TrustManager[] trustManagers = new TrustManager[]{new X509TrustManager() {
@Override
public X509Certificate[] getAcceptedIssuers() {
X509Certificate[] x509Certificates = new X509Certificate[0];
return x509Certificates;
}
@Override
public void checkServerTrusted(final X509Certificate[] chain,
final String authType) throws CertificateException {
System.out.println(TAG + ": authType: " + String.valueOf(authType));
}
@Override
public void checkClientTrusted(final X509Certificate[] chain,
final String authType) throws CertificateException {
System.out.println(TAG + ": authType: " + String.valueOf(authType));
}
}};
X509TrustManager x509TrustManager = new X509TrustManager() {
@Override
public X509Certificate[] getAcceptedIssuers() {
X509Certificate[] x509Certificates = new X509Certificate[0];
return x509Certificates;
}
@Override
public void checkServerTrusted(final X509Certificate[] chain,
final String authType) throws CertificateException {
System.out.println(TAG + ": authType: " + String.valueOf(authType));
}
@Override
public void checkClientTrusted(final X509Certificate[] chain,
final String authType) throws CertificateException {
System.out.println(TAG + ": authType: " + String.valueOf(authType));
}
};
OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder();
try {
final String PROTOCOL = "SSL";
SSLContext sslContext = SSLContext.getInstance(PROTOCOL);
KeyManager[] keyManagers = null;
SecureRandom secureRandom = new SecureRandom();
sslContext.init(keyManagers, trustManagers, secureRandom);
SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
okHttpClientBuilder.sslSocketFactory(sslSocketFactory, x509TrustManager);
} catch (Exception e) {
e.printStackTrace();
}
HostnameVerifier hostnameVerifier = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
System.out.println(TAG + ": hostname: " + String.valueOf(hostname));
if (hostname.equals(HOST)) {
return true;
}
return false;
}
};
okHttpClientBuilder.hostnameVerifier(hostnameVerifier);
OkHttpClient okHttpClient = okHttpClientBuilder.build();
try {
MultipartBody.Builder mbBuilder = new MultipartBody.Builder();
mbBuilder.setType(MultipartBody.FORM);
mbBuilder.addFormDataPart("id", "user_one");
RequestBody requestBody = mbBuilder.build();
Request.Builder requestBuilder = new Request.Builder();
requestBuilder.url(BASE_URL);
requestBuilder.post(requestBody);
Request request = requestBuilder.build();
Call call = okHttpClient.newCall(request);
Response response = call.execute();
if (response.isSuccessful()) {
String responseBody = response.body().string();
return responseBody;
} else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment