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
Posts: 3
Registered: ‎10-02-2008
My Device: Not Specified

HTTPConnection Related

public String send() throws Exception { HttpConnection hc = null; InputStream is = null; OutputStream dout= null; String res = null; ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { hc = (HttpConnection) Connector.open(url); hc.setRequestProperty("X-Action", "Upload"); hc.setRequestMethod(HttpConnection.POST); dout = hc.openOutputStream(); dout.write(postBytes); dout.flush(); //Step 2 int responseCode = hc.getResponseCode(); res = hc.getResponseMessage(); // STEP 3 } catch(Exception e) { System.out.println("Exception caught1: " + e); e.printStackTrace(); } finally { if(bos != null) bos.close(); if(is != null) is.close(); if(hc != null) hc.close(); if(dout!= null) dout.close(); } return res; }

 

Above Code runs fine until "STEP2", then it starts to wait.
After couple minutes, an exception is caught java.io.InterruptedIOException.

then it sends the body of the message, "postBytes" to the server.

 Server receives the message fine and reply it back with 200 OK.

 

I have read so many things about HTTPConnection, many problems, many suggestions.

I have tried executing the code

1. in a seperate thread

2.  to flush it and not flushing it.

3. synchronizing it, etc. 

Even though they do not seem to be related, I have tried them.

 

Does anyone know what is going on?

Developer
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: HTTPConnection Related

The server isn't really obligated to do anything until certain conditions are met on the request.

I'm assuming you have an http server and not a home brew system. Since you aren't

closing the connection or setting a length there is no obvious cue for the server to respond.

 

I've never tried to pipeline connections but I would imagine that setting the content length

without closing streams should get a response. Alternatively, just closing the stream will probably

provoke a response.

 

New Developer
Posts: 3
Registered: ‎10-02-2008
My Device: Not Specified

Re: HTTPConnection Related

Let me clarify the point little bit more.

 

It is a home brew server, and it does wait for the length to reply. length is supplied by the protocol and it is correct (checked).

 

the problem is that the above code does not send the body of the message until it is interrupted. 

(Server never receives the body of the message after receiving the headers until device is interrupted)

 

 

Developer
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: HTTPConnection Related

I guess you could try closing the output stream.

But, I would also be careful about assuming where the problem is. I routinely use a PERL "fake server" and

I don't see all my output until someone does something- before you conclude it hasn't been sent, make

sure your "server" output has been flushed too. A packet capture may help if in doubt.

 

Have you tried wget or something similar to post to your server?

 

New Developer
Posts: 3
Registered: ‎10-02-2008
My Device: Not Specified

Re: HTTPConnection Related

I have tried closing the output stream, too.

and yes, I have tried other methods to test the server. The server is very solid commercial server, and tested in so many other ways. It is currently used by other devices from other platforms without any problems. Server reads the headers and the content length, and then waits for the incoming packets. During the wait, server even displays the size of the read bytes, which made it easy to debug our applications when sending huge files.

 

When I say it -waits-, I am talking about a state that there is absolutely NO traffic between the device and the server. I am not even talking about seeing output or not in the server code, I am talking about the traffic in the network here. Device does not send anything, it waits.

 

I don't believe this is as simple as primitive flush issue. It looks like the application is blocked or something...

 ========

I was testing it on 4.2 device, now just tested this in a new 4.5.

 

there are several different things.

- content size is reported differently. 

- does not get interrupted, but it also does not send the packets.

there are some other behavioral changes in 4.5 that I will take a look at it.

 

 

 

Developer
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: HTTPConnection Related

I've used this on a few devices and not encountered a problem AFAIK,

 

conn.setRequestProperty(
             HttpProtocolConstants.HEADER_CONTENT_LENGTH,
              String.valueOf(postData.length));
         debug_loc=10;
         OutputStream out = null;   
     try {
     out = conn.openOutputStream();
    out.write(postData);
    out.flush();
    out.close();
    debug_loc=11;