views:

29

answers:

1

Hello Every one

i am using the following code for establishing Https connection

    HttpsConnection httpConnector = null;
    InputStream in = null;
    Document doc ;
    String content = "";

            try 
                {
               httpConnector = (HttpsConnection)Connector.open(url,Connector.READ_WRITE);
                    httpConnector.setRequestMethod(HttpConnection.GET) ;
                    in = httpConnector.openInputStream();
                    byte[] data = new byte[in.available()];
                    int len = 0;
                    int size = 0;
                    StringBuffer raw = new StringBuffer();
                        while ( -1 != (len = in.read(data)) ) {
                            raw.append(new String(data, 0, len));
                            size += len;
                        }
                        content = raw.toString().trim();
                                }
                   catch(Exception ex)
                   {
                       ex.printStackTrace();
                       return false;  
                   }
                   try{
                    in.close();
                    in =null;
                    httpConnector.close();
                    httpConnector =null;

            }catch(Exception ex)
            {
                Dialog.alert("Error:" + ex.getMessage());
                return false;

            } 
            } 

i think i am able to establish the connection but the values are not coming. i am testing it on Simulator, i have not tested on device

A: 

I think your mistake is in the following line:

byte[] data = new byte[in.available()];

The available() method only returns how many bytes are immediately available for reading from the inputstream, but you are using it to initialize the size of the temporary byte array. Since it's possible that available() returns 0, you may be initializing a zero-length array.

It would be better to just initialize "data" with a fixed-length array.

Marc Novakowski
but this code is working fine with HTTPconnection
rupesh
It may just be a coincidence of timing that HTTP connections have a positive value for available() whereas HTTPS perhaps is returning 0. It wouldn't hurt to try and just replace it with a fixed-length byte array.
Marc Novakowski
i think there is no getLength() for inputstream
rupesh
I replaced in.available() with some static value say 1024, still nothing is displayed.
rupesh
A few followup questions: is this a self-signed SSL site or signed by a trusted CA such as Verisign? Is it throwing any exceptions? Can you try it on a real device?
Marc Novakowski
i am able to see the value i have used 100000 as a static value but the problem i dont want to keep the static value, so wat's the alterntive reason for that
rupesh
You can use a small value such as 1024 - it's only a temporary buffer to read bytes into from the inputstream. If it's not big enough to hold your entire response body, it will just keep looping doing more reads (and adding data to your StringBuffer). Once the JVM leaves that code block the byte array will no longer be references and thus can be garbage collected, so don't worry about it using up memory.
Marc Novakowski
You may also want to take a look at the IOUtilities class for an easier way to read an InputStream into a byte array without having to set up temporary buffers, loops, etc.
Marc Novakowski