Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 59c06f2

Browse files
author
Alex Gotev
committed
Added HTTPS support
1 parent 18884bc commit 59c06f2

File tree

10 files changed

+76
-8
lines changed

10 files changed

+76
-8
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
eclipse.preferences.version=1
2+
encoding//src/com/alexbbb/uploadservice/AllCertificatesTruster.java=UTF-8

bin/android-upload-service.jar

2.14 KB
Binary file not shown.

bin/jarlist.cache

Lines changed: 0 additions & 3 deletions
This file was deleted.

bin/res/drawable-hdpi/ic_launcher.png

-8.98 KB
Binary file not shown.

bin/res/drawable-ldpi/ic_launcher.png

-4.94 KB
Binary file not shown.

bin/res/drawable-mdpi/ic_launcher.png

-4.94 KB
Binary file not shown.
-13.7 KB
Binary file not shown.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.alexbbb.uploadservice;
2+
3+
import java.security.SecureRandom;
4+
import java.security.cert.CertificateException;
5+
import java.security.cert.X509Certificate;
6+
import java.util.logging.Level;
7+
import java.util.logging.Logger;
8+
9+
import javax.net.ssl.HttpsURLConnection;
10+
import javax.net.ssl.SSLContext;
11+
import javax.net.ssl.TrustManager;
12+
import javax.net.ssl.X509TrustManager;
13+
14+
/**
15+
* TrustManager that accepts all certificates.
16+
*
17+
* @author Alex Gotev
18+
*/
19+
public class AllCertificatesTruster implements TrustManager, X509TrustManager {
20+
21+
private static final Logger LOGGER = Logger.getLogger(AllCertificatesTruster.class.getName());
22+
private static final String SSL_FAILED = "Unable to initialize the Trust Manager to trust all the "
23+
+ "SSL certificates and HTTPS hosts.";
24+
25+
@Override
26+
public final void checkClientTrusted(final X509Certificate[] xcs, final String string) throws CertificateException {
27+
}
28+
29+
@Override
30+
public final void checkServerTrusted(final X509Certificate[] xcs, final String string) throws CertificateException {
31+
}
32+
33+
@Override
34+
public final X509Certificate[] getAcceptedIssuers() {
35+
return new X509Certificate[0];
36+
}
37+
38+
public static void trustAllSSLCertificates() {
39+
final TrustManager[] trustAllCerts = new TrustManager[] {new AllCertificatesTruster()};
40+
41+
try {
42+
final SSLContext context = SSLContext.getInstance("SSL");
43+
context.init(null, trustAllCerts, new SecureRandom());
44+
HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
45+
} catch (Exception e) {
46+
LOGGER.log(Level.INFO, SSL_FAILED);
47+
}
48+
}
49+
}

src/com/alexbbb/uploadservice/UploadRequest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ public void validate() throws IllegalArgumentException, MalformedURLException {
4747
throw new IllegalArgumentException("Request URL cannot be either null or empty");
4848
}
4949

50-
if (url.startsWith("https")) {
51-
throw new IllegalArgumentException("HTTPS is not supported yet");
50+
if (!url.startsWith("http")) {
51+
throw new IllegalArgumentException("Specify either http:// or https:// as protocol");
5252
}
5353

5454
//Check if the URL is valid

src/com/alexbbb/uploadservice/UploadService.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77
import java.io.UnsupportedEncodingException;
88
import java.net.HttpURLConnection;
99
import java.net.MalformedURLException;
10-
import java.net.ProtocolException;
1110
import java.net.URL;
1211
import java.util.ArrayList;
1312

13+
import javax.net.ssl.HostnameVerifier;
14+
import javax.net.ssl.HttpsURLConnection;
15+
import javax.net.ssl.SSLSession;
16+
1417
import android.app.IntentService;
1518
import android.app.NotificationManager;
1619
import android.content.Intent;
@@ -189,8 +192,25 @@ private byte[] getTrailerBytes(final String boundary)
189192

190193
private HttpURLConnection getMultipartHttpURLConnection(final String url,
191194
final String boundary)
192-
throws IOException, ProtocolException {
193-
final HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
195+
throws IOException {
196+
final HttpURLConnection conn;
197+
198+
if (url.startsWith("https")) {
199+
AllCertificatesTruster.trustAllSSLCertificates();
200+
final HttpsURLConnection https = (HttpsURLConnection) new URL(url).openConnection();
201+
https.setHostnameVerifier(new HostnameVerifier() {
202+
203+
@Override
204+
public boolean verify(String hostname, SSLSession session) {
205+
return true;
206+
}
207+
208+
});
209+
conn = https;
210+
211+
} else {
212+
conn = (HttpURLConnection) new URL(url).openConnection();
213+
}
194214

195215
conn.setDoInput(true);
196216
conn.setDoOutput(true);

0 commit comments

Comments
 (0)