1

In the university we must develop an android application, which grabs our marks from the website.

We use the JSoup Framework to grab and parse the HTML pages. The problem now is that our University uses a self-signed certificate for HTTPS connection, which is not trusted by a larger trusted CA.

I've seen for these frequently occurring problems, some general solutions, but they use a extended DefaultHttpClient with a own SSLSocketFactory. These solutions do not look very nice, is there a simple beautiful solution for this by usage of the JSoup Framework?

private LoginState connect(String username, String password) {
        try {
            if (isOnline()) {
                Log.i(TAG, "Sending POST.");

                Connection connection = Jsoup.connect(LOGIN_URL)
                        .data(USER_FIELD, username).data(PASS_FIELD, password)
                        .data(LOGIN_BUTTON, "Anmelden")
                        .data(LOGIN_TYPE, "login").data(PID, "2")
                        .timeout(DEFAULT_TIMEOUT);

                mDocument = connection.post();

                mCookies.putAll(connection.response().cookies());

                if (isConnected()) {
                    Log.i(TAG, "Login successful.");
                    setState(LoginState.LOGIN_SUCCESSFUL);
                } else {
                    Log.i(TAG, "Login failed.");
                    setState(LoginState.LOGIN_FAILED);
                }
            } else {
                Log.i(TAG, "No Internet Connection.");
                setState(LoginState.BAD_CONNECTION);
            }
        } catch (IOException ex) {
            Log.e(TAG, ex.getStackTrace().toString());
            setState(LoginState.BAD_CONNECTION);
        }

        return getState();
    }
Name is Nilay
  • 2,743
  • 4
  • 35
  • 77
Pyth0n
  • 355
  • 5
  • 13
  • Related/possible duplicate: http://stackoverflow.com/a/7745706 – BalusC Dec 09 '11 at 23:49
  • Thank you @Craigy, but how I solve the following Exception "javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found." for the site [dhbw-loerrach.de(https://www.dhbw-loerrach.de/)? I've followed the instructions and wrote me for testing a small java application, but on android will it not run and ends with the above exception. Is there an error with the filetype or in the usage of it under Android 2.3? – Pyth0n Dec 12 '11 at 19:25
  • @Pyth0n you should probably ask a new question about that specific issue – skynet Dec 12 '11 at 19:28

1 Answers1

1

Unless JSoup exposes some API that lets your pass your own trust store, no. If you have a rooted device you can install the university's cert to the trust store using this. If you have an Android 4.0 (ICS) device, there is an UI for installing trusted CA certificates. Another idea: use HttpClient to get the data, and then use JSoup for parsing only (that should be possible).

Nikolay Elenkov
  • 52,576
  • 10
  • 84
  • 84