Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Contributor
hannahb
Posts: 4
Registered: ‎11-30-2012
My Device: Bold

HttpsConnection

[ Edited ]

Hi! I'm working on an app that requires the user to log in. I'm trying to use HttpsConnection to submit the username and password to a website that will verify the username/password. I haven't been able to find any tutorials so I've been trying to piece together sample code I've found, but I really have no idea what I'm doing. When I run this code, the whole application just freezes. Any help would be greatly appreciated!

 

 

    void postViaHttpsConnection() throws IOException 
    {
        HttpsConnection c = null;
        int rc;
        String url = "https://www.loginwebsite.com";

        try 
        {
            c = (HttpsConnections)Connector.open(url);

            // set the request method and headers
            c.setRequestMethod(HttpsConnection.POST);
            c.setRequestProperty("id""authenId");
            c.setRequestProperty("type""text");
            c.setRequestProperty("name""_authen_id");
            c.setRequestProperty("value""00000000"); // THIS IS THE USERNAME
            c.setRequestProperty("size""40");
            c.setRequestProperty("tabindex""10");
            c.setRequestProperty("maxlength""71");
            
            // getting the response code will open the connection, send the request, and read the HTTP response headers
            // the headers are stored until requested
            rc = c.getResponseCode();
            if (rc != HttpsConnection.HTTP_OK) 
            {
                throw new IOException("HTTP response code: " + rc);
            }
        } 
        
        catch (ClassCastException e) 
        {
            throw new IllegalArgumentException("Not an HTTP URL");
        }
    }
New Developer
larissartemis
Posts: 25
Registered: ‎12-03-2012
My Device: Playbook

Re: HttpsConnection

Like the app freezes, you can use System.out.println() in lines to see WHERE the app is locked. Maybe help to debug your app :smileyhappy:

New Contributor
hannahb
Posts: 4
Registered: ‎11-30-2012
My Device: Bold

Re: HttpsConnection

[ Edited ]

Thanks. I know that the problem occurs in the following line:

 

c = (HttpsConnections)Connector.open(url);
Contributor
Napolean
Posts: 11
Registered: ‎11-28-2012
My Device: Curve 9320

Re: HttpsConnection

Hi

One reason for the app freezing could be u running app on UI thread.
Perform this network checking and connection establishment in a separate Thread.

For more you can check the following links:

http://supportforums.blackberry.com/t5/forums/searchpage/tab/message?filter=location&location=forum-...

Trusted Contributor
gopi
Posts: 126
Registered: ‎10-15-2010
My Device: Torch 9850

Re: HttpsConnection

Follow this link
http://supportforums.blackberry.com/t5/Java-Development/Sample-HTTP-Connection-code-and-BIS-B-Access...

Try to implement the Transport Detective class to find out available transport and getFullURL funtion before opening the Https connection, since I referred the Peter's sample for https connection and it worked great for my https connection.
String _connectionURL = getFullURL(url, _transportType);
c = (HttpsConnections)Connector.open(_connectionURL);

private static String getFullURL(String requestedURL, int transport) {
        URLFactory urlFactory = new URLFactory(requestedURL);
        if ( (transport & TransportDetective.TRANSPORT_MDS) > 0 ) {
            return urlFactory.getHttpMdsUrl(false);
        } else
        if ( (transport & TransportDetective.TRANSPORT_TCP_WIFI) > 0 ) {
            return urlFactory.getHttpTcpWiFiUrl();
        } else
        if ( (transport & TransportDetective.TRANSPORT_BIS_B) > 0 ) {
            return urlFactory.getHttpBisUrl();
        } else
        if ( (transport & TransportDetective.TRANSPORT_TCP_CELLULAR) > 0 ) {
            // Only here for Simulator
            return urlFactory.getHttpDefaultUrl() + ";deviceside=true";
        } else {
            return urlFactory.getHttpDefaultUrl();
        }
    }

 

New Developer
larissartemis
Posts: 25
Registered: ‎12-03-2012
My Device: Playbook

Re: HttpsConnection

Good tips, guys! hannahb, and now? The connection works?