Browse Source

SSL Image Download done(Not tested)

old
Yiğit Çolakoğlu 6 years ago
parent
commit
9c0f93f101
3 changed files with 79 additions and 48 deletions
  1. +42
    -11
      MyCity/app/src/main/java/gq/yigit/mycity/tools/ImageDownload.java
  2. +36
    -8
      MyCity/app/src/main/java/gq/yigit/mycity/tools/MySSLSocketFactory.java
  3. +1
    -29
      MyCity/app/src/main/java/gq/yigit/mycity/tools/WebRequest.java

+ 42
- 11
MyCity/app/src/main/java/gq/yigit/mycity/tools/ImageDownload.java View File

@ -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<String, Void, Bitmap> {
@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<String, Void, Bitmap> {
public void addListener(imageListener toAdd) {
listeners.add(toAdd);
}
}

+ 36
- 8
MyCity/app/src/main/java/gq/yigit/mycity/tools/MySSLSocketFactory.java View File

@ -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();
}
}
}

+ 1
- 29
MyCity/app/src/main/java/gq/yigit/mycity/tools/WebRequest.java View File

@ -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<Void,Void,String> {
public WebRequest(String url, boolean request_type, HashMap<String,String> request_content){
client = getNewHttpClient();
client = MySSLSocketFactory.getNewHttpClient();
this.url = url;
this.request_content = request_content;
@ -116,29 +111,6 @@ public class WebRequest extends AsyncTask<Void,Void,String> {
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();
}
}
}

Loading…
Cancel
Save