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
Highlighted
New Developer
Posts: 8
Registered: ‎03-28-2009
My Device: Not Specified
Accepted Solution

Problem with POST when using HttpsConnection

Hello Community,

 

my scenario:

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;

 

https.setRequestMethod(HttpsConnection.POST);

 

https.setRequestProperty(HEADER_CONTENTTYPE, CONTENTTYPE_FORMDATA);

 

https.setRequestProperty(HEADER_CONTENTTYPE, String.valueOf(length));

 

out = https.openOutputStream();

out.write(PostMessage.getBytes());

in = https.openInputStream();

 

ResponseMessage = new byte[(int)https.getLength()];

 

in.read(ResponseMessage);

 

out.close();

in.close();

https.close();

 

Any help is highly appreciated!!!

 

Thanks in advance.

 

PS:

 

Simulator: 8300

MDS: 4.1.4

JDE: 4.5.0

Eclipse: Version: 3.4.1
           Build id: M20080911-1700 

New Developer
Posts: 8
Registered: ‎03-28-2009
My Device: Not Specified

Re: Problem with POST when using HttpsConnection

Dear Community,

 

I managed to fix the issue by myself. Its a classic copy&paste error.

 

Lets have a look at:

https.setRequestProperty(HEADER_CONTENTTYPE, CONTENTTYPE_FORMDATA);

 

https.setRequestProperty(HEADER_CONTENTTYPE, String.valueOf(length));

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.

New Developer
Posts: 7
Registered: ‎04-29-2009
My Device: Not Specified

Re: Problem with POST when using HttpsConnection

Hi Beggin4More,

 

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.

 

 

Thanks

Chhavi

New Developer
Posts: 8
Registered: ‎03-28-2009
My Device: Not Specified

Re: Problem with POST when using HttpsConnection

Hy Chhavi,

 

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:

  1. BB Side: Convert the Image from byte[] to a base64 string
  2. BB Side: use a POST to send the information, like you would post regular form data
  3. Server Side: convert base64 to byte[] to image

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.

 

Bye ;-)

 

 

 

New Developer
Posts: 7
Registered: ‎04-29-2009
My Device: Not Specified

Re: Problem with POST when using HttpsConnection

Hi Beggin4More,

 

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;
try
{
String stringImage=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);
hc.setRequestProperty("User-Agent",System.getProperty("microedition.profiles"));
hc.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
hc.setRequestProperty("Content-Length", Integer.toString(stringImage.length()));
hc.setRequestMethod(HttpConnection.POST);
// Obtain the output stream for the HttpConnection
out = hc.openOutputStream();
out.write(stringImage.getBytes());
out.flush();
in = hc.openInputStream();
data = new byte[(int)hc.getLength()];
in.read(data);
}
catch(IOException e)
{
getDisplay().setCurrent(get_exception());
exception.setTitle("IO Exception!");
exception.setString("Cannot connect to network, Please try again later."+e.toString());
e.printStackTrace();
}

 

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.

 

Thanx

 

Chhavi

 

New Developer
Posts: 8
Registered: ‎03-28-2009
My Device: Not Specified

Re: Problem with POST when using HttpsConnection

Hey Chhavi,

 

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&param2=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:

out.close();
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 = .

 

Bye!

 

 

 

New Developer
Posts: 7
Registered: ‎04-29-2009
My Device: Not Specified

Re: Problem with POST when using HttpsConnection

hello Beggin4More,

 

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

 

 

out.flush();

 to

 

 

out.close();

 

 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

 

 

Chhavi

 

 

 

New Developer
Posts: 8
Registered: ‎03-28-2009
My Device: Not Specified

Re: Problem with POST when using HttpsConnection

[ Edited ]

Well you are opening an input stream so your servlet should return something...

 

Could you post the servlet? Would be a great help.

 

Bye

 

PS: Where do you close the input stream and the connection (BB Side)?

Message Edited by Beggin4More on 04-30-2009 05:55 AM
New Developer
Posts: 7
Registered: ‎04-29-2009
My Device: Not Specified

Re: Problem with POST when using HttpsConnection

Hi  Beggin4More,

 

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..

 

 

Thanx

Chhavi

 

 

New Developer
Posts: 8
Registered: ‎03-28-2009
My Device: Not Specified

Re: Problem with POST when using HttpsConnection

Whats the size in Kbyte of your image?