12-03-2012 12:55 PM - edited 12-03-2012 01:17 PM
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");
}
}
12-03-2012 02:39 PM
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 ![]()
12-03-2012 04:30 PM - edited 12-03-2012 04:30 PM
Thanks. I know that the problem occurs in the following line:
c = (HttpsConnections)Connector.open(url);
12-04-2012 12:52 AM
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/sear
12-04-2012 12:58 AM
Follow this link
http://supportforums.blackberry.com/t5/Java-Develo
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();
}
}
12-04-2012 10:44 PM