Performing login to https website via Android app

2020-02-17 04:53发布

问题:

First off, I'm pretty newb at this. I'm new to Android, to asp, to javascript, to http even.

I'm trying to build an Android app that allows me to login to my school's website and pull data off it, ultimately I hope to do something like insert my timetable's data into Android's calendar entries. However I'm having trouble logging in.

Here's the website: https://sso.wis.ntu.edu.sg/webexe88/owa/sso_login2.asp

What I'm doing currently is doing a http POST to the above-mentioned URL and I'm hoping to be redirected to hhttps://wish.wis.ntu.edu.sg/pls/webexe/aus_stars_check.check_subject_web2 which will display my timetable.

So far my code is as follows after viewing the webpage's source and searching quite a bit on the Internet:

private void start_login(String[] array) {
    // TODO Auto-generated method stub
    Toast.makeText(this, "Logging in...", Toast.LENGTH_LONG).show();

    WebView wv = new WebView(this);     
    this.setContentView(wv);

    try {

        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
        nameValuePairs.add(new BasicNameValuePair("UserName", <my username here>));
        nameValuePairs.add(new BasicNameValuePair("PIN", <my password here>));
        nameValuePairs.add(new BasicNameValuePair("Domain", "STUDENT"));
        nameValuePairs.add(new BasicNameValuePair("p2", "https://wish.wis.ntu.edu.sg/pls/webexe/aus_stars_check.check_subject_web2"));

        wv.loadData(CustomHttpClient.executeHttpPost(URL, nameValuePairs), "text/html", "utf-8");

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}// end start_login

That's the login function.

The CustomHttpClient I'm using is thanks to this guy: http://www.newtondev.com/2010/07/27/making-http-requests-using-google-android/

So far I'm not getting any results. What am I doing wrong? Am I missing values in the ArrayList, or have I got the URL all wrong?

回答1:

Below code handles https and gives httpsclient for https url .. you need httpsclient to make request to https urls.

Might below code is of help to you:

public DefaultHttpClient getClient() 
   {
        DefaultHttpClient ret = null;

        //sets up parameters
        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, "utf-8");
        params.setBooleanParameter("http.protocol.expect-continue", false);

        //registers schemes for both http and https
        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        final SSLSocketFactory sslSocketFactory = SSLSocketFactory.getSocketFactory();
        sslSocketFactory.setHostnameVerifier(SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
        registry.register(new Scheme("https", sslSocketFactory, 443));

        ThreadSafeClientConnManager manager = new ThreadSafeClientConnManager(params, registry);
        ret = new DefaultHttpClient(manager, params);
        return ret;
    }