From 9c0f93f1011554f26fbbe37000b29563693fba19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yi=C4=9Fit=20=C3=87olako=C4=9Flu?= Date: Mon, 25 Mar 2019 12:44:49 +0300 Subject: [PATCH] SSL Image Download done(Not tested) --- .../gq/yigit/mycity/tools/ImageDownload.java | 53 +++++++++++++++---- .../mycity/tools/MySSLSocketFactory.java | 44 ++++++++++++--- .../gq/yigit/mycity/tools/WebRequest.java | 30 +---------- 3 files changed, 79 insertions(+), 48 deletions(-) diff --git a/MyCity/app/src/main/java/gq/yigit/mycity/tools/ImageDownload.java b/MyCity/app/src/main/java/gq/yigit/mycity/tools/ImageDownload.java index 054c563..52ef951 100644 --- a/MyCity/app/src/main/java/gq/yigit/mycity/tools/ImageDownload.java +++ b/MyCity/app/src/main/java/gq/yigit/mycity/tools/ImageDownload.java @@ -4,8 +4,29 @@ import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.AsyncTask; import android.util.Log; +import cz.msebera.android.httpclient.HttpEntity; +import cz.msebera.android.httpclient.HttpResponse; +import cz.msebera.android.httpclient.HttpVersion; +import cz.msebera.android.httpclient.StatusLine; +import cz.msebera.android.httpclient.client.HttpClient; +import cz.msebera.android.httpclient.client.methods.HttpGet; +import cz.msebera.android.httpclient.client.methods.HttpUriRequest; +import cz.msebera.android.httpclient.conn.ClientConnectionManager; +import cz.msebera.android.httpclient.conn.scheme.PlainSocketFactory; +import cz.msebera.android.httpclient.conn.scheme.Scheme; +import cz.msebera.android.httpclient.conn.scheme.SchemeRegistry; +import cz.msebera.android.httpclient.conn.ssl.SSLSocketFactory; +import cz.msebera.android.httpclient.impl.client.DefaultHttpClient; +import cz.msebera.android.httpclient.impl.conn.tsccm.ThreadSafeClientConnManager; +import cz.msebera.android.httpclient.params.BasicHttpParams; +import cz.msebera.android.httpclient.params.HttpParams; +import cz.msebera.android.httpclient.params.HttpProtocolParams; +import cz.msebera.android.httpclient.protocol.HTTP; +import cz.msebera.android.httpclient.util.EntityUtils; +import java.io.IOException; import java.io.InputStream; +import java.security.KeyStore; import java.util.ArrayList; import java.util.List; @@ -17,20 +38,29 @@ public class ImageDownload extends AsyncTask { @Override protected Bitmap doInBackground(String... URL) { + try { + String imageURL = URL[0]; + HttpUriRequest request = new HttpGet(imageURL.toString()); + HttpClient httpClient = MySSLSocketFactory.getNewHttpClient(); + HttpResponse response = httpClient.execute(request); - String imageURL = URL[0]; - Log.d("[BOOKMARK]",imageURL); + StatusLine statusLine = response.getStatusLine(); + int statusCode = statusLine.getStatusCode(); + if (statusCode == 200) { + HttpEntity entity = response.getEntity(); + byte[] bytes = EntityUtils.toByteArray(entity); - Bitmap bitmap = null; - try { - // Download Image from URL - InputStream input = new java.net.URL(imageURL).openStream(); - // Decode Bitmap - bitmap = BitmapFactory.decodeStream(input); - } catch (Exception e) { - e.printStackTrace(); + Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, + bytes.length); + return bitmap; + }else{ + return null; + } + }catch (Exception e){ + Log.e("[ERROR]","Download failed because " + e.getMessage()); + return null; } - return bitmap; + } @Override @@ -42,4 +72,5 @@ public class ImageDownload extends AsyncTask { public void addListener(imageListener toAdd) { listeners.add(toAdd); } + } diff --git a/MyCity/app/src/main/java/gq/yigit/mycity/tools/MySSLSocketFactory.java b/MyCity/app/src/main/java/gq/yigit/mycity/tools/MySSLSocketFactory.java index 1bc78bd..344ae17 100644 --- a/MyCity/app/src/main/java/gq/yigit/mycity/tools/MySSLSocketFactory.java +++ b/MyCity/app/src/main/java/gq/yigit/mycity/tools/MySSLSocketFactory.java @@ -1,20 +1,24 @@ package gq.yigit.mycity.tools; -import android.util.Log; -import cz.msebera.android.httpclient.conn.ConnectTimeoutException; +import cz.msebera.android.httpclient.HttpVersion; +import cz.msebera.android.httpclient.client.HttpClient; +import cz.msebera.android.httpclient.conn.ClientConnectionManager; +import cz.msebera.android.httpclient.conn.scheme.PlainSocketFactory; +import cz.msebera.android.httpclient.conn.scheme.Scheme; +import cz.msebera.android.httpclient.conn.scheme.SchemeRegistry; import cz.msebera.android.httpclient.conn.ssl.SSLSocketFactory; -import org.apache.http.conn.scheme.LayeredSocketFactory; -import org.apache.http.params.HttpConnectionParams; -import org.apache.http.params.HttpParams; +import cz.msebera.android.httpclient.impl.client.DefaultHttpClient; +import cz.msebera.android.httpclient.impl.conn.tsccm.ThreadSafeClientConnManager; +import cz.msebera.android.httpclient.params.BasicHttpParams; +import cz.msebera.android.httpclient.params.HttpParams; +import cz.msebera.android.httpclient.params.HttpProtocolParams; +import cz.msebera.android.httpclient.protocol.HTTP; import javax.net.ssl.SSLContext; -import javax.net.ssl.SSLSocket; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import java.io.IOException; -import java.net.InetAddress; -import java.net.InetSocketAddress; import java.net.Socket; import java.net.UnknownHostException; import java.security.*; @@ -52,4 +56,28 @@ public class MySSLSocketFactory extends SSLSocketFactory { public Socket createSocket() throws IOException { return sslContext.getSocketFactory().createSocket(); } + + public static HttpClient getNewHttpClient() { + try { + KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); + trustStore.load(null, null); + + MySSLSocketFactory sf = new MySSLSocketFactory(trustStore); + sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); + + HttpParams params = new BasicHttpParams(); + HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); + HttpProtocolParams.setContentCharset(params, HTTP.UTF_8); + + SchemeRegistry registry = new SchemeRegistry(); + registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 5000)); + registry.register(new Scheme("https", sf, 5000)); + + ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry); + + return new DefaultHttpClient(ccm, params); + } catch (Exception e) { + return new DefaultHttpClient(); + } + } } \ No newline at end of file diff --git a/MyCity/app/src/main/java/gq/yigit/mycity/tools/WebRequest.java b/MyCity/app/src/main/java/gq/yigit/mycity/tools/WebRequest.java index 44716c2..40407c4 100644 --- a/MyCity/app/src/main/java/gq/yigit/mycity/tools/WebRequest.java +++ b/MyCity/app/src/main/java/gq/yigit/mycity/tools/WebRequest.java @@ -1,6 +1,5 @@ package gq.yigit.mycity.tools; -import android.net.wifi.WifiConfiguration; import android.os.AsyncTask; import android.util.Log; import cz.msebera.android.httpclient.HttpResponse; @@ -17,17 +16,13 @@ import cz.msebera.android.httpclient.conn.scheme.Scheme; import cz.msebera.android.httpclient.conn.scheme.SchemeRegistry; import cz.msebera.android.httpclient.conn.ssl.SSLSocketFactory; import cz.msebera.android.httpclient.impl.client.DefaultHttpClient; -import cz.msebera.android.httpclient.impl.client.HttpClientBuilder; import cz.msebera.android.httpclient.impl.conn.tsccm.ThreadSafeClientConnManager; import cz.msebera.android.httpclient.message.BasicNameValuePair; import cz.msebera.android.httpclient.params.BasicHttpParams; import cz.msebera.android.httpclient.params.HttpParams; import cz.msebera.android.httpclient.params.HttpProtocolParams; import cz.msebera.android.httpclient.protocol.HTTP; -import gq.yigit.mycity.MainActivity; -import gq.yigit.mycity.R; -import javax.net.ssl.*; import java.io.*; import java.security.KeyStore; import java.util.*; @@ -46,7 +41,7 @@ public class WebRequest extends AsyncTask { public WebRequest(String url, boolean request_type, HashMap request_content){ - client = getNewHttpClient(); + client = MySSLSocketFactory.getNewHttpClient(); this.url = url; this.request_content = request_content; @@ -116,29 +111,6 @@ public class WebRequest extends AsyncTask { public void addListener(responseListener toAdd) { listeners.add(toAdd); } - public HttpClient getNewHttpClient() { - try { - KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); - trustStore.load(null, null); - - MySSLSocketFactory sf = new MySSLSocketFactory(trustStore); - sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); - - HttpParams params = new BasicHttpParams(); - HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); - HttpProtocolParams.setContentCharset(params, HTTP.UTF_8); - - SchemeRegistry registry = new SchemeRegistry(); - registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 5000)); - registry.register(new Scheme("https", sf, 5000)); - - ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry); - - return new DefaultHttpClient(ccm, params); - } catch (Exception e) { - return new DefaultHttpClient(); - } - } }