tags:

views:

314

answers:

2

How to connect via http to the server to do implement login

A: 

Go through http demo sample code provided with JDE

imMobile
A: 

I presume you need to do a POST login. The following is a snippet of what you can do:

javax.microedition.io.HttpConnection connection = null;

try {
    net.rim.blackberry.api.browser.URLEncodedPostData encoder = new net.rim.blackberry.api.browser.URLEncodedPostData(null, false);
    encoder.append(/* put your username field name eg. */ "username", /* put username value here eg. */ "username");
    encoder.append(/* put your password field name eg. */ "password", /* put your password value here eg. */ "password");
    /* add additional data to be POSTed */

    connection = (javax.microedition.io.HttpConnection)javax.microedition.io.Connector.open(/* put the POST URL eg. */ "http://www.facebook.com/login.php");
    connection.setRequestMethod(javax.microedition.io.HttpConnection.POST);
    connection.setRequestProperty(net.rim.device.api.io.http.HttpProtocolConstants.HEADER_CONTENT_TYPE, net.rim.device.api.io.http.HttpProtocolConstants.CONTENT_TYPE_APPLICATION_X_WWW_FORM_URLENCODED);
    connection.setRequestProperty(net.rim.device.api.io.http.HttpProtocolConstants.HEADER_CONTENT_LENGTH, String.valueOf(encoder.getBytes().length));

    java.io.OutputStream os = connection.openOutputStream();
    os.write(encoder.getBytes());

    if (connection.getResponseCode() == javax.microedition.io.HttpConnection.HTTP_OK) {
        /* process response here */
    }
} catch (java.io.IOException e) {
} finally {
    if (connection != null) {
        try { connection.close(); } catch (IOException e) {}
    }
}
Eki