03-28-2009 01:57 PM
I have a piece of code running on a BB and a Servlet located on a Tomcat 6.0.18. The code on the BB uses POST to send data. When using POST with HTTP everything works like a charm, but I have trouble with POST and HTTPS.
It seems the data is not transfered from BB to the server. Using the debugger I found out that the data is written on the output stream (device side), but when the servlet invokes getParameter() only null is returned.
To ensure a correct setup I tried a GET with HTTPS which works just fine.
The given code example is from the device side
connection = Connector.open(url);
HttpsConnection https = (HttpsConnection)connection;
out = https.openOutputStream();
in = https.openInputStream();
ResponseMessage = new byte[(int)https.getLength()];
Any help is highly appreciated!!!
Thanks in advance.
Eclipse: Version: 3.4.1
Build id: M20080911-1700
Solved! Go to Solution.
03-28-2009 02:43 PM
I managed to fix the issue by myself. Its a classic copy&paste error.
Lets have a look at:
As you can see I used HEADER_CONTENTTYPE twice. Its not that bad, but to indicate the length of a post one should use HEADER_CONTENTLENGTH :-D.
I should take a break....
You can consider the issue as solved.
04-29-2009 08:06 AM
I m facing the issue somwhat related to yours but its not like paste error. ActuIlay i m porting an application in blackberry 8100 Pearl, and it is working fine on simulator. But when i try to send data to my servlet on server through mobile phone, it gives me exception of "java.io.InterruptedIOException: Connection timed out". the data i m trying to send is encoded form of image data as byte array.
Please help me out as i m nt getting the reason that why the connection times out as GPRS is working ok in other modules of application.
04-29-2009 09:14 AM
without code example I can only guess ;-)
Double check your request header. Do you use the correct content type for sending picture?
To be honest I dont know the content type for sending a picture, but I would try the following:
Be carefull with the datatype of the image. I dont know if your server can distinguish between jpg or png for example.
You can send the datatype as string and evaluate it on server side.
One last tip, post source code! Its more helpful and I am sure you will receive more answers.
04-30-2009 03:40 AM
thanx fr ur reply.. here is the part of my code where i m trying to send stringImage (which isBase64 encoded) using POST method...
HttpConnection hc = null;
OutputStream out = null;
InputStream in = null;
byte data = null;
//enode the image data by the Base64 algorithm
stringImage = Base64.encode(image_data);
// url is URL of the Sevlet
// Obtain an HTTPConnection
hc = (HttpConnection) Connector.open(url);
// Obtain the output stream for the HttpConnection
out = hc.openOutputStream();
in = hc.openInputStream();
data = new byte[(int)hc.getLength()];
exception.setString("Cannot connect to network, Please try again later."+e.toString());
But where i m facing problem is that application keep on trying to flush data on output stream and then throws an IOException of InterruptedIOException: Connection timed out. But the gprs service is running well on other application modules.
Is there is any limit of data that can be uploaded from blackberry..?? or what is happening..??
I m working on BB 8100 4.2.0 version and the application is working fine on its simulator. And this application is also running fine on other phone models of Nokia and sony ericsson.
Please help me out, i m not able to grab the reason.
04-30-2009 04:35 AM
sorry for the missunderstanding.
Well there is a upload limit but AFAIK it depends on the used MDS settings.
For detailed information you should contact the MDS administrator our your blackberry support contact.
After reviewing your code I have a question regarding your variable stringImage.
Is the value only the base64 representation of the images byte?
AFAIK you have to pass a parameter name also, e.g. param1=value1¶m2=value2.
But I guess the real problem is the following
out.flush(); in = hc.openInputStream();
flush() usually writes data on stream, but the stream is still open.
Try using out.close() instead. close() flushes the stream and closes it.
So try the following instead:
in = hc.openInputStream();
At the moment you try to keep the out active and also create an input stream.
Seems to be reason for the TimeOutException.
Again double check the content of stringImage. It should contain a parameter name and a base64 string which are separated by = .
04-30-2009 05:48 AM
thanx for the info..,
For your question: Is the value only the base64 representation of the images byte? ya.. i hvn't passed any parameter name as i m writing the stringImage value on output stream and in servlet on my server i m evaluating the stringImage value through inputstream.
After changing the code line from
the application gives Exception of Bad DNS address first time. And after restarting application, it indicates me that it has sent data to server but i hvn't recieved anything there. And then it again keep on waiting... nd at last throws exception of Connection timed out.
please help me out.. thanx
04-30-2009 05:53 AM - edited 04-30-2009 05:55 AM
Well you are opening an input stream so your servlet should return something...
Could you post the servlet? Would be a great help.
PS: Where do you close the input stream and the connection (BB Side)?
04-30-2009 06:02 AM
in servlet, i m just opening a new input stream and reading the data in buffer and the creating the image.. nd at last returning the name with which image is saved..
InputStream in = request.getInputStream();
BufferedReader r = new BufferedReader(new InputStreamReader(in));
nd this code is working with simulator bt not on real device.. i m nt getting the reason fr this..