I have been struggling the past few days to track down why I consistently get these exceptions within my application when attempting to use HTTPConnection. I have scoured numerous forums and no one seem to be having the same problem as me. I would appreciate any insight anyone might have.
I am using a makeConnection function based on the Browser sample code, but it seems to be very unreliable. The code funtions 100% on the simulator but on the device itself I can only get some of the connections to connect and return data. I make two types of requests, a few POST requests to our server and then some GET requests to retrieve image data from amazon s3 servers. It seems like the first few initail requests to our server work fairly reliably, then exceptions start occuring.
The requests to amazon fail consistently with "net.rim.device.cldc.io.dns.DNSException: Bad DNS Address". However, I am certain that the address being requested is valid, because the image loads perfectly in the blackberry browser when the URL is entered.
The request to our server seems to fail when multiple connections are attempted or connections are attempted too frequently and usually fail with "net.rim.device.cldc.io.dns.DNSException: Port is already bound: 19780" but have recieved other exceptions as well such as "java.io.IOException: Open tunnel - max timeout" and "java.io.IOException: Unable to open connection."
The application runs in a multi-threaded environment where each HTTPConnection request is handled by one of 5 worker threads, which means we could have up to 5 active connections at any given time. Below you will find the code used to create the connection(almost exactly out of the JDE browser sample code with the addtion of getBES() to set connection string), the Exceptions are thrown on the Connector.open(url) call
Details:
Device: 8310 on ATT running V4.2.2.170 (Platform 2.5.0.30) without MDS just Blackberry Internet Service. There is no firewall enabled, the appliation has been granted all permissions, The device has EDGE coverage with 4 bars of signal and the Blackberry Browser is able to hit all of the links my application can not.
I am using JDE 4.2.1 to build and sign the code.
Now for code snippet:
Code:
public static HttpConnection makeConnection(String inUrl, HttpHeaders requestHeaders, byte[] postData) {
String url = inUrl + getBES(); //NOTE this is using deviceside=true
HttpConnection conn = null;
try {
if (requestHeaders != null) {
//
// Clients SHOULD NOT include a Referer header field in a (non-secure) HTTP
// request if the referring page was transferred with a secure protocol.
String referer = requestHeaders.getPropertyValue("referer");
boolean sendReferrer = true;
if (referer != null && StringUtilities.startsWithIgnoreCase(referer, "https:") && !StringUtilities.startsWithIgnoreCase(url, "https:")) {
sendReferrer = false;
}
int size = requestHeaders.size();
for (int i = 0; i < size;) {
String header = requestHeaders.getPropertyKey(i);
// Remove referer header if needed.
if (!sendReferrer && header.equals("referer")) {
requestHeaders.removeProperty(i);
--size;
continue;
}
String value = requestHeaders.getPropertyValue(i++);
if (value != null) {
conn.setRequestProperty(header, value);
}
}
}
//make sure a user-agent is set, harcode the user agent the blackberry browser on simulator uses.
if(conn != null)
conn.setRequestProperty("User-Agent", "BlackBerry8300/4.5.0.44 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/-1");
if (postData == null) {
conn.setRequestMethod(HttpConnection.GET);
} else {
conn.setRequestMethod(HttpConnection.POST);
conn.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LENGTH, String.valueOf(postData.length));
out = conn.openOutputStream();
out.write(postData);
}
}
catch (Exception e1){
System.out.println(Thread.currentThread().getName() + " - Utilities:makeConnection Exception(" + conn + "): " + e1.toString());;
}
finally {
if (out != null) {
try {
out.close();
} catch (IOException e2) {
}
}
}
return conn;
}
private static String getBES() {
ServiceRecord[] ippprecordArray = ServiceBook.getSB().findRecordsByCid("IPPP");
if (ippprecordArray == null) {
return null;
}
int numRecords = ippprecordArray.length;
for (int i = 0; i < numRecords; i++) {
ServiceRecord ipppRecord = ippprecordArray[i];
if (ipppRecord.isValid()) {
int encryptionMode = ipppRecord.getEncryptionMode();
if (encryptionMode == ServiceRecord.ENCRYPT_RIM) {
return ";deviceside=false";
}
}
}
//use apn instead
return ";deviceside=true";
}