Skip to content

Commit

Permalink
Some changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ABPSoft committed Apr 4, 2019
1 parent 3db5e4f commit ed56021
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,31 +1,46 @@
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;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.TlsVersion;
import okio.BufferedSink;

/**
Expand All @@ -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;
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -181,7 +198,9 @@ public void run()


}
catch(final Exception e)
catch(
final Exception e)

{
e.printStackTrace();

Expand All @@ -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="";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -76,6 +78,6 @@ public void onError(int errorCode,String errorText,Exception e)
}
});

abpWebService.sendRequest(null,object.toString());
abpWebService.sendRequest("test",object.toString());
}
}

0 comments on commit ed56021

Please sign in to comment.