Skip to content

Instantly share code, notes, and snippets.

@MikolajKakol
Created October 9, 2014 13:46
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MikolajKakol/d32395ad040a67396711 to your computer and use it in GitHub Desktop.
Save MikolajKakol/d32395ad040a67396711 to your computer and use it in GitHub Desktop.
Proxy+gradle+OkHttp config
buildTypes {
release {
buildConfigField "boolean", "OMIT_HTTPS_SECURITY", "false"
buildConfigField "String", "HTTP_PROXY_INET", "null"
buildConfigField "int", "HTTP_PROXY_PORT", "0"
}
debug {
buildConfigField "boolean", "OMIT_HTTPS_SECURITY", "true"
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
buildConfigField "String", "HTTP_PROXY_INET", properties.getProperty('proxy.inet', 'null')
buildConfigField "int", "HTTP_PROXY_PORT", properties.getProperty('proxy.port', '0')
}
}
proxy.inet=192.168.10.134
proxy.port=8888
@Provides
@Singleton
OkHttpClient okHttpClient() {
OkHttpClient client = new OkHttpClient();
if (BuildConfig.OMIT_HTTPS_SECURITY) {
ignoreHttpsSecurity(client);
}
//noinspection ConstantConditions
if (BuildConfig.HTTP_PROXY_INET != null) {
client.setProxy(new Proxy(Proxy.Type.HTTP, InetSocketAddress.createUnresolved(BuildConfig.HTTP_PROXY_INET, BuildConfig.HTTP_PROXY_PORT)));
}
return client;
}
private void ignoreHttpsSecurity(OkHttpClient client) {
TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException {
}
public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException {
}
}
};
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new SecureRandom());
client.setSslSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
e.printStackTrace();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment