Hi guys,
Does anyone out there have a working example of how to keep an HTTP connection open during multiple requests and responses?
The second time that I write to the outputstream, no bytes is send to the server (yes I remember to flush :-)
Maybe I've got it all wrong, but unfortunately it seems that the 7210 does not support keeping connections alive. I must get a new connection in order to perform a new post.
Here is a snapshot of the sourcecode from my test client, that polls the server each 5th second:
// Open connection
// ======================================
try {
connection = (HttpConnection) Connector.open("http://127.0.0.1:8020", Connector.READ_WRITE);
connection.setRequestMethod(HttpConnection.POST);
}
catch(Exception e) {
Alert alert = new Alert("Network error", e.getMessage(),
null, AlertType.ERROR);
Display.getDisplay(this).setCurrent(alert);
e.printStackTrace();
}
// --------------------------------------
boolean openStreams = true;
while(true) {
try {
// Do post
if(openStreams){
out = connection.openDataOutputStream();
}
out.write(new String("Test post!".getBytes());
out.flush();
// Read HttpResponse content
if(openStreams){
in = connection.openInputStream();
}
int len = (int) connection.getLength();
in.read(buffer, 0, len);
openStreams = false;
}
catch(Exception e) {
Alert alert = new Alert("Network error", e.getMessage(),
null, AlertType.ERROR);
Display.getDisplay(this).setCurrent(alert);
e.printStackTrace();
}
try {
Thread.sleep(5000);
}
catch(InterruptedException ie) {
}
}

.getBytes());
Reply With Quote

