Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Developer
cckellogg
Posts: 2
Registered: ‎10-16-2008
My Device: Not Specified

Persistent Http Connection

Has anyone successfully implemented a persistent http connection?

 

I have a simple program that tries to open a persistent http connection and then makes 10 requests (same url) to our server. I have read most of the developer docs and watched the videos and my understanding is that http connections are persistent by default. However, I am experiencing different behavior.

 

Below is the code.

 

 

Thread t = new Thread() {
public void run() {
int length = REQUESTS.length;
for(int i=0; i<length; ++i) {
System.out.println("URL Number " + (i+1));
final String url = REQUESTS[i];
HttpRequest request = new HttpRequest(url);
request.run();
}
}
};
t.start();

private final class HttpRequest implements Runnable {

private final String url;

HttpRequest(final String url) {
this.url = url;
}

public void run() {

HttpConnection hc = null;
DataInputStream din = null;

try {
String s = url;
if(DeviceInfo.isSimulator()) {
s = s + ";deviceside=true";
}

System.out.println("Connecting to " + s);

hc = (HttpConnection) Connector.open(s);


hc.setRequestMethod(HttpConnection.GET);
hc.setRequestProperty("Connection","Keep-Alive");

int responseCode = hc.getResponseCode();
String responseMessage = hc.getResponseMessage();
System.out.println("Response Code " + responseCode);
System.out.println("Response Message " + responseMessage);

din = hc.openDataInputStream();
int size = (int)hc.getLength();
System.out.println("Conent Length " + size);
byte[] data = null;
if(-1 != size) {
data = new byte[size];
din.readFully(data);
}


// print response
System.out.println(new String(data));

} catch (Throwable t) {
System.out.println("HttpRequest Error: " + t.toString());
t.printStackTrace();
} finally {
if(null != din) {
try {
din.close();
} catch(Exception e) {
}
}

if(null != hc) {
try {
hc.close();
} catch(Exception e) {
}
}
}

}

}

 

I have run a tcp dump on the server and the connection is closed after each request. Also, I have done a tcp dump while running the program on the simulator and the results are the same, a new connection is created each time.  I have also tried not to close the http connection, but that did not work either. 

 

I am using the eclipse Component Pack 4.3.0_4.3.0.13 for the simulator testing.

 

Here is the details of the phone that I am testing on:

 

Network: Sprint

 

Model: Pearl 8130

 

8130 on Sprint network

 

OS Version: 4.3.0.127

 

 

Does anyone have any ideas or suggestions?

 

Thanks.

Please use plain text.
Developer
marchywka
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: Persistent Http Connection

did you notice the get header? Itis 1.0 AFAIK.

 

http://tools.ietf.org/rfc/rfc2616.txt

 

but, see all the other comments about pipelining versus burst modes over wireless.

 

Please use plain text.
New Developer
cckellogg
Posts: 2
Registered: ‎10-16-2008
My Device: Not Specified

Re: Persistent Http Connection

Here is the tcp dump from the program:

 

 

(the request to the server)

GET /get/ HTTP/1.1
Host: mysite.com
Connection: Keep-Alive


(the reponse)
HTTP/1.1 200 OK
Date: Mon, 09 Feb 2009 17:03:28 GMT
Server: Apache/2.2.6 (Fedora)
Vary: Accept-Encoding
Content-Length: 1244
Keep-Alive: timeout=300, max=100
Connection: Keep-Alive
Content-Type: text/html;charset=ISO-8859-1

 

 

The connection is still being dropped each time.  Any ideas? 

 

Thanks.

 

 

Please use plain text.
Developer
RexDoug
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: Persistent Http Connection

I've never really had a requirement for a persistent HTTP connection. The HTTP transactions are stateless and atomic, so what does keeping the connection "open" do for your app?

 

 

 

 

 

Please use plain text.
Developer
marchywka
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: Persistent Http Connection

I thought all the BB requests, probably from reading these boards,  even on later devices were 1.0 but I see looking through my own

logs that may not be exactly true.

On the 4.7 docs I don't see anyway to set this so you would suspect it would default to one or the other.

 

Anyway, from my own logs this seems to be a "via" issue ( see comments form ietf below).

I have 1.0 and 1.1 coming from same device except for the "via" header- anything between you and

phone has liberty to change things and it sounds like my statement is almost correct but it isn't phones

as much as RIM servers that could be the issue,

 

$ more xxxx.xxx | grep "...\.\.td" | grep "protocol\|Agent\|Via" | highlight "HTTP\/1.0" | more

 

 

User-Agent: BlackBerry8330/4.3.0 Profile/MIDP-2.0Configuration/CLDC-1.1 VendorID/105
protocol: HTTP/1.1
User-Agent: BlackBerry8330/4.3.0 Profile/MIDP-2.0Configuration/CLDC-1.1 VendorID/105
Via: BISB_3.4.0.37, 1.1 pmds113.bisb1.blackberry:3128 (squid/2.5.STABLE12)
protocol: HTTP/1.0
User-Agent: BlackBerry8330/4.3.0 Profile/MIDP-2.0Configuration/CLDC-1.1 VendorID/105
protocol: HTTP/1.1
User-Agent: BlackBerry8330/4.3.0 Profile/MIDP-2.0Configuration/CLDC-1.1 VendorID/105

 

 

There is some discsussion on sun.com going back many years but you will probably have to sort through

it and figure it out. There are comments about MIDP 1.x supporting http 1.1 explicitly for pipelining

etc and more comments about persistent connections with http 1.0(?).

 

http://java.sun.com/j2se/1.5.0/docs/guide/net/http-keepalive.html

http://forums.sun.com/thread.jspa?threadID=354952&tstart=12510

 

you can get some idea of the differences here, in particular you'd have to think about various proxies and carrier or RIM servers , as these can be an issue,

 

http://www.google.com/search?hl=en&q=site%3Aietf.org+http%2F1.0+keep-alive

Please use plain text.