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
PocariSweat1991
Posts: 4
Registered: ‎07-18-2009
My Device: Not Specified

ConnectionClosedException when POSTing images via HTTPS

Greetings,

I am trying to send images to a server from my Verizon BlackBerry 8330 using an HTTPS connection. I am performing the following steps:

HttpsConnection https = (HttpsConnection)Connector.open("https://myserver");

https.setRequestMethod(HttpsConnection.POST);
https.setRequestProperty("Content-Type", "image/jpg");

FileConnection file = (FileConnection)Connector.open("file:///filename", Connector.READ_WRITE);

InputStream is = file.openInputStream();
OutputStream os = https.openOutputStream();

byte[] b = new byte[1024];
for(int i; (i = is.read(b)) != -1;) {
os.write(b, 0, i);
}

int code = https.getResponseCode();

 

I am receiving a ConnectionClosedException when I call getResponseCode() after the final write() to the OutputStream, but I can't think of anything in my code that is closing the connection. 

 

Does anyone know what could be causing the connection to be closed between the time that I open the connection and finish writing to its OutputStream?

Thank you.

Please use plain text.
Developer
BBDeveloper
Posts: 3,951
Registered: ‎07-15-2008
My Device: Not Specified

Re: ConnectionClosedException when POSTing images via HTTPS

IS your application able to connect to your server using http connection? Is it only problem with https?

Use Search. "Accept Solution" If the problem is resolved.
Please use plain text.
New Developer
PocariSweat1991
Posts: 4
Registered: ‎07-18-2009
My Device: Not Specified

Re: ConnectionClosedException when POSTing images via HTTPS

BB Developer,

 

Thank you for your response.  The application is able to connect to the server using both HTTP and HTTPS.  However, the ConnectionClosedException is only occurring when I POST the image using an HTTPS connection.

 

The server does have its SSL certificate installed.  

Please use plain text.
Developer
BBDeveloper
Posts: 3,951
Registered: ‎07-15-2008
My Device: Not Specified

Re: ConnectionClosedException when POSTing images via HTTPS

Thanks for sharing the solution.

Use Search. "Accept Solution" If the problem is resolved.
Please use plain text.
New Developer
PocariSweat1991
Posts: 4
Registered: ‎07-18-2009
My Device: Not Specified

Re: ConnectionClosedException when POSTing images via HTTPS

My question is, what in my code is causing the HTTPS connection to close when I call getResponseCode().

 

If the same code works for HTTP, could anyone teach my why it fails for HTTPS?

 

Thank you. 

Please use plain text.
Developer
rjones
Posts: 77
Registered: ‎10-07-2008
My Device: Bold 9000

Re: ConnectionClosedException when POSTing images via HTTPS

Did you ever find the solution to your problem? I'm having the same issue, but it is not an option for me to use http. I must use https.
Please use plain text.
Regular Contributor
lyhoanghai
Posts: 71
Registered: ‎03-24-2010
My Device: 9550
My Carrier: Vodaphone

Re: ConnectionClosedException when POSTing images via HTTPS

[ Edited ]

I have the same problem.

Have anyone successfully POST with HTTPS in blackberry?

 

(I also set Content length

conn.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LENGTH, ""+postBytes.length);

in my  code but it does not work)

 

Please use plain text.
Developer
arkadyz
Posts: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Re: ConnectionClosedException when POSTing images via HTTPS

In your situation, when the last write() throws this exception, it is probably benign - the server accepted the last expected bytes and closed the connection.  Some servers just do that.  Catch that exception, report it if you so desire - and that's all.

 

I had something similar when the last read() threw that exception.  Disregarding it worked fine.  I talked to our server people and they claim that HTTP servers sometimes do just that to save going back and forth...

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
Please use plain text.
Developer
Ted_Hopp
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: ConnectionClosedException when POSTing images via HTTPS

I'd suggest two changes to your code:

 

  1. Set the content-length header on the HTTPS connection (file.fileSize() will give you the value to use). Some servers won't properly process POSTs that are missing this header.
  2. Call os.flush() (or, perhaps better, os.close()) before calling getResponseCode(), to make sure that all data goes to the server instead of sitting in a buffer somewhere.

I don't know if these changes will fix your problem, but they are definitely worth doing anyway.




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Please use plain text.
Developer
Eugen
Posts: 467
Registered: ‎07-16-2009
My Device: Z10 LE
My Carrier: Vodafone NL

Re: ConnectionClosedException when POSTing images via HTTPS

Got email with log from customer today. The same - ConnectionClosedException.

Yes I set up content length HTTP header.

Yes I do flush before asking HTTP code response.

 

Going to investigate server logs for clear understanding what cause is.

Please use plain text.