From ed56021ea55c3080e56dd5c60a43e1415493c290 Mon Sep 17 00:00:00 2001 From: Amin Bahrami Date: Fri, 5 Apr 2019 00:12:46 +0430 Subject: [PATCH] Some changes --- .../abpwebservice/ABPWebService.java | 93 ++++++++++++++++--- .../abpwebservicelib/ActivityMain.java | 6 +- 2 files changed, 83 insertions(+), 16 deletions(-) diff --git a/ABPWebService/src/main/java/com/aminbahrami/abpwebservice/ABPWebService.java b/ABPWebService/src/main/java/com/aminbahrami/abpwebservice/ABPWebService.java index 9483c30..0d9cc7b 100644 --- a/ABPWebService/src/main/java/com/aminbahrami/abpwebservice/ABPWebService.java +++ b/ABPWebService/src/main/java/com/aminbahrami/abpwebservice/ABPWebService.java @@ -1,24 +1,38 @@ package com.aminbahrami.abpwebservice; +import android.os.Build; import android.os.Handler; -import android.util.Base64; import android.util.Log; import android.webkit.MimeTypeMap; -import java.io.BufferedReader; -import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; -import java.io.InputStreamReader; -import java.io.OutputStreamWriter; -import java.net.HttpURLConnection; -import java.net.MalformedURLException; -import java.net.URL; +import java.io.InputStream; +import java.security.KeyStore; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; +import java.security.cert.CertificateFactory; +import java.security.cert.X509Certificate; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; import java.util.concurrent.TimeUnit; +import javax.net.ssl.HostnameVerifier; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLSession; +import javax.net.ssl.SSLSocketFactory; +import javax.net.ssl.TrustManager; +import javax.net.ssl.TrustManagerFactory; +import javax.net.ssl.X509TrustManager; +import javax.security.cert.CertificateException; + import okhttp3.Call; import okhttp3.Callback; +import okhttp3.CipherSuite; +import okhttp3.ConnectionSpec; import okhttp3.FormBody; import okhttp3.MediaType; import okhttp3.MultipartBody; @@ -26,6 +40,7 @@ import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; +import okhttp3.TlsVersion; import okio.BufferedSink; /** @@ -50,10 +65,14 @@ public void setReadTimeout(int readTimeout) private IOnNetwork iOnNetwork=null; private String url=""; + private boolean isSsl=false; + public ABPWebService setUrl(String url) { this.url=url; + isSsl=url.toLowerCase().startsWith("https"); + return this; } @@ -119,11 +138,9 @@ public void writeTo(BufferedSink sink) throws IOException try { - OkHttpClient client=new OkHttpClient.Builder() - .connectTimeout(connectTimeout,TimeUnit.MILLISECONDS) - .readTimeout(readTimeout,TimeUnit.MILLISECONDS) - .writeTimeout(readTimeout,TimeUnit.MILLISECONDS) - .build(); + + OkHttpClient client=getNewClient(); + Request request=new Request.Builder() .url(url) @@ -181,7 +198,9 @@ public void run() } - catch(final Exception e) + catch( + final Exception e) + { e.printStackTrace(); @@ -203,6 +222,52 @@ public void run() thread.start(); } + private OkHttpClient getNewClient() + { + OkHttpClient.Builder builder=new OkHttpClient.Builder() + .connectTimeout(connectTimeout,TimeUnit.MILLISECONDS) + .readTimeout(readTimeout,TimeUnit.MILLISECONDS) + .writeTimeout(readTimeout,TimeUnit.MILLISECONDS) + .followRedirects(true) + .followSslRedirects(true) + .retryOnConnectionFailure(true) + .cache(null); + + + if(isSsl && (Build.VERSION.SDK_INT >= 17 && Build.VERSION.SDK_INT<22)) + { + try + { + ConnectionSpec spec=new ConnectionSpec.Builder(ConnectionSpec.COMPATIBLE_TLS) + .supportsTlsExtensions(true) + .tlsVersions(TlsVersion.TLS_1_2,TlsVersion.TLS_1_1,TlsVersion.TLS_1_0) + .cipherSuites( + CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, + CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, + CipherSuite.TLS_DHE_RSA_WITH_AES_128_GCM_SHA256, + CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, + CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, + CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, + CipherSuite.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, + CipherSuite.TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, + CipherSuite.TLS_ECDHE_RSA_WITH_RC4_128_SHA, + CipherSuite.TLS_DHE_RSA_WITH_AES_128_CBC_SHA, + CipherSuite.TLS_DHE_DSS_WITH_AES_128_CBC_SHA, + CipherSuite.TLS_DHE_RSA_WITH_AES_256_CBC_SHA) + .build(); + + builder.connectionSpecs(Collections.singletonList(spec)); + } + catch(Exception exc) + { + Log.e("OkHttpTLSCompat","Error while setting TLS 1.2",exc); + } + } + + + return builder.build(); + } + private static String getMimeType(String url) { String type=""; diff --git a/app/src/main/java/com/aminbahrami/abpwebservicelib/ActivityMain.java b/app/src/main/java/com/aminbahrami/abpwebservicelib/ActivityMain.java index 0a3825b..58fd6dd 100644 --- a/app/src/main/java/com/aminbahrami/abpwebservicelib/ActivityMain.java +++ b/app/src/main/java/com/aminbahrami/abpwebservicelib/ActivityMain.java @@ -38,8 +38,10 @@ private void sendRequest() { ABPWebService abpWebService=new ABPWebService(); //abpWebService.setUrl("https://14bazikon.com/api/users/login"); - abpWebService.setUrl("http://api.apiservice.info/"); + //abpWebService.setUrl("http://api.apiservice.info/"); + abpWebService.setUrl("https://api.apiservice.info/"); //abpWebService.setUrl("https://api.github.com/repos/square/okhttp/issues"); + //abpWebService.setUrl("https://reqres.in/api/users"); //Check Permission in the feature File file=new File(Environment.getExternalStorageDirectory()+"/test.jpg"); @@ -76,6 +78,6 @@ public void onError(int errorCode,String errorText,Exception e) } }); - abpWebService.sendRequest(null,object.toString()); + abpWebService.sendRequest("test",object.toString()); } }