开发者

Need help: How to make a http connection in Blackberry to login to any server?

How to connect via ht开发者_如何转开发tp to the server to do implement login


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

import javax.microedition.io.HttpConnection;
import net.rim.blackberry.api.browser.URLEncodedPostData;
import net.rim.device.api.io.http.HttpProtocolConstants;

HttpConnection connection = null;

try {
    URLEncodedPostData encoder = new 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 = (HttpConnection)javax.microedition.io.Connector.open(
            /* put the POST URL eg. */ "http://www.facebook.com/login.php");
    connection.setRequestMethod(HttpConnection.POST);
    connection.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_TYPE, 
        HttpProtocolConstants.CONTENT_TYPE_APPLICATION_X_WWW_FORM_URLENCODED);
    connection.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LENGTH, 
        String.valueOf(encoder.getBytes().length));

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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜