BlackBerry Forums Support Community
              

Closed Thread
 
Thread Tools
Old 09-30-2008, 02:48 PM   #1
thegman
New Member
 
Join Date: Sep 2008
Model: 8800
PIN: N/A
Carrier: Vodafone
Posts: 3
Default HTTPSConnection problems

Please Login to Remove!

Hello all,
I'm trying to get started in a little BB programming, and my first task is to make a HTTPS connection to a server, but the below code (which I took from an example on these forums) does not work for me, either on the simulator or on my actual BB 8800.

When run, I get my "Could not connect...", but the printing of the stack trace simply says "no stack trace", which is not very helpful.

Can anyone show me the error of my ways? Code is paste below. Note that I've taken out the actual URL as this forum will not let me post a link if I've got fewer than 10 posts under my belt, and I guess the parser has not noticed that the URL is in the CODE tags.

Cheers

Garry

Code:
 public void createConnection() {
                StreamConnection s = null;
                HttpsConnection connection = null;
                OutputStream outputStream = null;
                String result = new String();
                String httpConnString = new String();
                try {

                        httpConnString = "the URL goes here";
                        connection = (HttpsConnection) Connector.open(httpConnString);

                        connection.setRequestMethod(HttpsConnection.POST);

                        outputStream = connection.openOutputStream();

                        outputStream.write(httpConnString.getBytes());


                        InputStream inputStream = connection.openInputStream();
                        byte[] responseData = new byte[10000];
                        int length = 0;
                        StringBuffer rawResponse = new StringBuffer();
                        while (-1 != (length = inputStream.read(responseData))) {
                                rawResponse.append(new String(responseData, 0, length));
                        }

                     

                   
                        int responseCode = connection.getResponseCode();
                        if (responseCode != HttpConnection.HTTP_OK) {
                                throw new IOException("HTTP response code: " + responseCode);
                        }

                        
                         result = rawResponse.toString();
                         net.rim.device.api.ui.component.Dialog.alert(result);

                } catch (Exception e) { 
                    net.rim.device.api.ui.component.Dialog.alert("Could not connect...");
                    e.printStackTrace();
                 }   
    }
Offline  
Old 10-01-2008, 02:55 AM   #2
Ivanov
Talking BlackBerry Encyclopedia
 
Join Date: Apr 2008
Location: Germany, BW
Model: -
PIN: N/A
Carrier: -
Posts: 310
Default

which exception is it?

Code:
Dialog.alert("Could not connect... " + e + ": " + e.getMessage());
__________________
Blessed is the end user who expects nothing, for he/she will not be disappointed. (Franklin's Rule)
Offline  
Old 10-01-2008, 01:55 PM   #3
thegman
New Member
 
Join Date: Sep 2008
Model: 8800
PIN: N/A
Carrier: Vodafone
Posts: 3
Default

Hi Ivanov,
Thanks for taking the time to help, here is the response I get:

Could not connect... java.io.IOException: Failed to transmit: Failed to transmit

That's on the simulator.

Any ideas?

Cheers

Garry
Offline  
Old 12-03-2008, 06:42 AM   #4
BB1364
Thumbs Must Hurt
 
Join Date: Oct 2007
Model: 7100i
PIN: N/A
Carrier: Dont know
Posts: 195
Default

Hi

I am facing similar problem while using https and POST method if i use GET method it wrks fine, but i need POST.

Does anyone have any ideas.


Thnaks
Offline  
Old 12-03-2008, 07:02 AM   #5
Ivanov
Talking BlackBerry Encyclopedia
 
Join Date: Apr 2008
Location: Germany, BW
Model: -
PIN: N/A
Carrier: -
Posts: 310
Default

try to change HttpsConnection to HttpConnection but provide the https:// protocol in the connection string.
__________________
Blessed is the end user who expects nothing, for he/she will not be disappointed. (Franklin's Rule)
Offline  
Old 12-03-2008, 10:31 AM   #6
ggovind
New Member
 
Join Date: Dec 2008
Model: Bold
PIN: N/A
Carrier: ATT
Posts: 7
Default ConnectionOpen(url) timed out..is there some device/service setup to be taken care?

I have problem in GET method also. My ConnectionOpen(url) Times out in BB Bold. I'm sure this is something to do with the device/service setup, because the code works fine in my simulator for the same URL.

-G


Quote:
Originally Posted by BB1364 View Post
Hi

I am facing similar problem while using https and POST method if i use GET method it wrks fine, but i need POST.

Does anyone have any ideas.


Thnaks
Offline  
Old 12-03-2008, 11:00 AM   #7
hrbuckley
BlackBerry Extraordinaire
 
Join Date: Jan 2006
Model: LEZ10
OS: 10.0.10
Carrier: Rogers CA
Posts: 1,704
Default

First when running on the simulator make sure you also have the MDS simulator running. Second HTTP transactions have to happen off the main execution thread.

Here is a code snippet from one of my apps that pulls objects off a queue and reports the content via HTTP GETs to a server.

Code:
 private class HttpThread extends Thread
 {
        ...
        public void run()
        {
            int returnCode = 0;
            try
            {
                while (outputQueSize() > 0)
                {
                    String returnContent, returnContentType;
                    
                    Object obj = peekOutputQue();
                    if (obj instanceof TrackPosition)
                    {
                        String url = makeUrl((TrackPosition)obj);
                        HttpConnection http = (HttpConnection)Connector.open(url);
            
                        returnCode = http.getResponseCode();
                        String returnMessage = http.getResponseMessage();
                        InputStream in = http.openInputStream();
                        
                        returnContentType = http.getType();
            
                        int cl = (int)http.getLength();
                        if (cl > 0)
                        {
                            byte[] buf = new byte[cl];
                            in.read(buf);
                            returnContent = new String(buf);
                        }
                        else
                        {
                            StringBuffer sb = new StringBuffer();
                            byte[] buf = new byte[1024];
                            int n = 0;
                            
                            while ((n = in.read(buf)) > 0)
                            {
                                sb.append(new String(buf,0,n));
                            }
                            returnContent = sb.toString();
                        }
                        
                        in.close();
                        http.close();
                    }
                    dequeueOutput();
                }
            }
            catch (Exception e)
            {
                System.err.println(e.toString());
                System.err.println(e.getMessage());
            }
            
            System.out.println(returnCode);
            done = true;
        }
    }
 }
Also, remember that unless you specify deviceside=true HTTPS connections are established from the Rim Operations Centre, which may not be what you want, and my not work depending on your server certificate. If you do set deviceside=true, then you may also need to set the APN on the device at Options -> Advanced Options -> TCP.
Offline  
Old 12-03-2008, 09:47 PM   #8
criccomini
New Member
 
Join Date: Dec 2008
Model: Curve
PIN: N/A
Carrier: Verizon
Posts: 2
Default

For the string URL, just use https://<blah blah> .. Make sure you have your MDS server running.

Code:
import java.io.InputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;

public class HTTPClient {
	public static String getPage(String url) {
		String response = "";

		try {
			StreamConnection s = (StreamConnection)Connector.open(url);

			InputStream input = s.openInputStream();

			byte[] data = new byte[256];
			int len = 0;
			StringBuffer raw = new StringBuffer();

			while( -1 != (len = input.read(data))) {
				raw.append(new String(data, 0, len));
			}

			response = raw.toString();

			input.close();
			s.close();
		} catch(Exception e) { }

		return response;
	}
}
Offline  
Old 12-08-2008, 02:30 AM   #9
BB1364
Thumbs Must Hurt
 
Join Date: Oct 2007
Model: 7100i
PIN: N/A
Carrier: Dont know
Posts: 195
Default

Do i need to make any changes in the MDS config file to enable HTTPS POST
Offline  
Closed Thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


ITENA Total C-Ram 8gm Automix Dual Cure self Adhesive Resin Luting Dental Cement picture

ITENA Total C-Ram 8gm Automix Dual Cure self Adhesive Resin Luting Dental Cement

$56.99



Fanuc S-RAM Memory Module A20B-3900-0284/01A *New No Box* picture

Fanuc S-RAM Memory Module A20B-3900-0284/01A *New No Box*

$154.95



50-TON HYDRAULIC RAM JACK porta power type cylinder lifting jacks rams picture

50-TON HYDRAULIC RAM JACK porta power type cylinder lifting jacks rams

$119.04



50-TON HYDRAULIC RAM JACK – porta power type cylinder – lifting jacks – rams picture

50-TON HYDRAULIC RAM JACK – porta power type cylinder – lifting jacks – rams

$119.04



CP-700 Handheld Hydraulic Pump Tool For 10-Ton Hydraulic Ram Cylinders Durable picture

CP-700 Handheld Hydraulic Pump Tool For 10-Ton Hydraulic Ram Cylinders Durable

$97.00



50 Ton Hydraulic Flat Jack Pancake Cylinder Ram 64MM for Machinery Industries US picture

50 Ton Hydraulic Flat Jack Pancake Cylinder Ram 64MM for Machinery Industries US

$103.32







Copyright © 2004-2016 BlackBerryForums.com.
The names RIM © and BlackBerry © are registered Trademarks of BlackBerry Inc.