0

My goal is to be able to login to the my3 website to parse data, but currently, I'm getting a "javax.net.ssl.SSLException: Not trusted server certificatie" error when trying to pull to login page.

I used the example source code from here which is supposed to ignore all SSL certificates, but I'm still getting the same error.

The code:

HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;

DefaultHttpClient client = new DefaultHttpClient();

SchemeRegistry registry = new SchemeRegistry();
SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
registry.register(new Scheme("https", socketFactory, 443));
SingleClientConnManager mgr = new SingleClientConnManager(client.getParams(), registry);
DefaultHttpClient httpClient = new DefaultHttpClient(mgr, client.getParams());

HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);

try {

    final String url = "https://sso.three.co.uk/mylogin/?service=https%3A%2F%2Fwww.three.co.uk%2FThreePortal%2Fappmanager%2FThree%2FSelfcareUk%3F_pageLabel%3DP22200581501288714397101%26_nfpb%3Dtrue%26&resource=portlet";
    HttpPost httpPost = new HttpPost(url);
    HttpResponse response = httpClient.execute(httpPost);

} catch(IOException e) {

    AlertDialog alertDialog = new AlertDialog.Builder(SslActivity.this).create();
    alertDialog.setTitle("About");
    alertDialog.setMessage(e.toString());
    alertDialog.setButton("Okay", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {

        }
    });

    alertDialog.show();

}
Community
  • 1
  • 1
92Jacko
  • 523
  • 2
  • 10
  • 18

1 Answers1

1

SSL on Android is a right pain.

I written a class for a library (GreenDroid) which makes a HttpClient which allows HTTPS without issues.

https://github.com/kennydude/GreenDroid/blob/master/GreenDroid/src/greendroid/util/HttpClientHelper.java

:)

Joe Simpson
  • 2,546
  • 4
  • 30
  • 46
  • Also, i'm being nosy but what are you connecting into 3UK for? – Joe Simpson Aug 10 '11 at 13:46
  • thank you, could you please provide a usage example for your class? (sorry, it's just that im still quite new to android developing and java) and my reason for wanting to connect is so that i can retrieve/parse my mobile usage information (: – 92Jacko Aug 10 '11 at 13:51
  • `code` HttpClientHelper.getTolerantClient(); DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); HttpResponse response = httpClient.execute(httpPost); `code` but this didnt solve the problem, am i doing something wrong? thanks (: – 92Jacko Aug 10 '11 at 14:52
  • I use it like this `DefaultHttpClient client = HttpClientHelper.getTolerantClient();` (please check my spelling, I'm doing this off the top of my head!) – Joe Simpson Aug 11 '11 at 17:47
  • thanks for getting back to me, i did try it like that, but it still doesn't work with the required url (https://sso.three.co.uk/mylogin/?service=https%3A%2F%2Fwww.three.co.uk%2FThreePortal%2Fappmanager%2FThree%2FSelfcareUk%3F_pageLabel%3DP22200581501288714397101%26_nfpb%3Dtrue%26&resource=portlet) – 92Jacko Aug 11 '11 at 19:10
  • Try in HttpClientHelper.java commenting out line 36 and 42 and see if that helps :) – Joe Simpson Aug 12 '11 at 13:14
  • still returns the error unfortunately :/ thanks for the suggestion though (: – 92Jacko Aug 12 '11 at 15:13
  • After googling, I found this http://stackoverflow.com/questions/995514/https-connection-android#1000205 -- might work? – Joe Simpson Aug 12 '11 at 15:16
  • using that code, it seems to be connecting to the site now, without throwing exceptions, but using the example code, im not sure of how to grab the page source (as i have only used jsoup upto now for grabbing source) any tips? (: – 92Jacko Aug 12 '11 at 15:53
  • I'm sorry, I don't deal with screen scrapping. By any chance are you making a balance query app for 3? :) – Joe Simpson Aug 13 '11 at 18:05
  • no problem, thanks for your help (: and yes i am (: i know there are a few out there already, but their quite crap if im being honest :P – 92Jacko Aug 17 '11 at 12:41
  • I'm not on 3, so I wouldn't know i'm on giffgaff – Joe Simpson Aug 18 '11 at 17:47