Our midlet reads data from our server. We need to use a secure connection, it all works fine (i.e. the connection is established, the server gets the request and responds) until we come to read the data from the connection.
The server response is read from the connection in a loop (see attached code example), the first read succeeds but subsequent reads fail with a " Secure read failed" exception.

It works fine with a socketconnection.

Does anyone have any advice on this problem?

Thanks



import java.io.*

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;

import org.kxml.*;
import org.kxml.kdom.*;
import org.kxml.io.*;

public class GPRSTestMidlet_1 extends MIDlet implements CommandListener
{
private static final Command ConnectCommand = new Command(" Connect" , Command.OK, 0);
private static final Command DisconnectCommand = new Command(" Disconnect" , Command.OK, 0);
private static final Command TransmitCommand = new Command(" Transmit" , Command.OK, 0);
private static final Command ReceiveCommand = new Command(" Receive" , Command.OK, 0);
private static final Command ExitCommand = new Command(" Exit" , Command.EXIT, 0);
private static final Command ClearCommand = new Command(" Clear" , Command.CANCEL, 0);

private static final String TheURL = " ssl://xxx.xxx.xxx.xxx" ;

Form f;

SocketConnection socketConnection;
InputStream is;
OutputStream os;

String req;

public GPRSTestMidlet_1()
{
f = new Form(" GPRSTestMidlet" );
f.addCommand(ConnectCommand);
f.addCommand(DisconnectCommand);
f.addCommand(TransmitCommand);
f.addCommand(ReceiveCommand);
f.addCommand(ClearCommand);
f.addCommand(ExitCommand);
f.setCommandListener(this);

try
{
Document doc = new Document();

// create XML content for server request


ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(baos);

doc.write(new XmlWriter(osw));

req = new String(baos.toByteArray());
}
catch (IOException ioe)
{}

Display.getDisplay(this).setCurrent(f);
}

public void commandAction(Command c, Displayable d)
{
if (c == ExitCommand)
{
destroyApp(false);
notifyDestroyed();
}

if (c == ClearCommand)
{
f.deleteAll();
}

if (c == ConnectCommand)
{
new ConnectionThread().start();
}

if (c == ReceiveCommand)
{
new ConnectionReceiveThread().start();
}

try
{
if (c == DisconnectCommand)
{
is.close();
os.close();
socketConnection.close();
f.append(" Disconnected\n" );
}

if (c == TransmitCommand)
{
//os = socketConnection.openOutputStream();

ByteArrayOutputStream buffer = null;
byte[]] buf = null;

buffer = new ByteArrayOutputStream();

buffer.write(new String(" POST " + " /" + " HTTP/1.0\n" ).getBytes());
buffer.write(new String(" Content-Length: " + req.length() + " \n" ).getBytes());
buffer.write(" Content-Type: text/xml\n" .getBytes());
buffer.write(" \n" .getBytes());

buffer.write(req.getBytes());

// send request
os.write(buffer.toByteArray());
os.flush();

f.append(new String(buffer.toByteArray()) + " \n" );
}

}
catch (IOException ioe)
{
f.append(ioe.getMessage() + " \n" );
}
Display.getDisplay(this).setCurrent(f);
}

public void startApp()
{
}

public void pauseApp()
{
}

public void destroyApp(boolean unconditional)
{
}

class ConnectionThread extends Thread
{
public void run()
{
try
{
f.append(" Connecting...\n" );
socketConnection = (SocketConnection)Connector.open(TheURL, Connector.READ_WRITE, true);
socketConnection.setSocketOption(SocketConnection.LINGE
R, 10);
f.append(" SNDBUF = " + socketConnection.getSocketOption(SocketConnection.SNDBU
F) + " \n" );
f.append(" RCVBUF = " + socketConnection.getSocketOption(SocketConnection.RCVBU
F) + " \n" );
os = socketConnection.openOutputStream();
is = socketConnection.openInputStream();
f.append(" Connected\n" );
}
catch (IOException ioe)
{
f.append(ioe.getMessage());
}
}
}

class ConnectionReceiveThread extends Thread
{
public void run()
{
try
{
//is = socketConnection.openInputStream();

ByteArrayOutputStream bos = new ByteArrayOutputStream();

/* This next piece of code is awork around for the problem
* but is not acceptable because the server response size
* may be larger than 2048
*/
/*
byte[]] buf = new byte[2048]];

boolean exception = false;
while(true & & !exception)
{
try
{
int rd = is.read(buf, 0, buf.length);
if (rd == -1) break;
bos.write(buf, 0, rd);
}
catch (IOException ioe)
{
if (ioe.getMessage().equals(" Secure read failed" ))
exception = true;
else
throw ioe;
}
}
*/

// The following fails because the second time the
// read method is called an exception is thrown
// N.B. this mechanism works for a non secure connection
byte[]] buf = new byte[256]];

while(true)
{
int rd = is.read(buf, 0, buf.length);
if (rd == -1) break;
bos.write(buf, 0, rd);
}

buf = bos.toByteArray();
f.append(" length = " + bos.size() + " \n" );
f.append(new String(buf));
}
catch (IOException ioe)
{
f.append(ioe.getMessage());
}
}
}
}