lostInTransit
lostInTransit

Reputation: 70997

Sending data in HTTP request's body fails

I am using the HttpConnection class of J2ME in my BlackBerry app to send data to a web server. I need to send the contents of an image in the body of the HTTP request.

This is what I do

  1. Get the bytes of the file in an array

  2. Open HTTP connection

  3. Set content type header as image/jpeg

  4. Get output stream of the connection

  5. Write the bytes to the output stream

  6. Close the output stream and connection

But the image is not uploaded to the server. What could be the problem?

Thanks.

EDIT - Adding code

HttpConnection conn = null;
OutputStream out = null;

try{
    conn = new HttpConnection(Connector.open(myURL));
    conn.setRequestProperty("Content-Type", "image/jpeg");
    conn.setRequestMethod(HttpConnection.POST);
    conn.setRequestProperty("Content-Disposition", "form-data");
    conn.setRequestProperty("Connection", "Keep-Alive");

    out = conn.openOutputStream;
    out.write(buffer, 0, buffer.length);
    conn.setRequestProperty("Content-Length", buffer.length);
    out.flush();
}
catch(Exception e){
    e.printStackTrace();
}
finally{
    if(out != null)
        out.close();

    if(conn != null){
        System.out.println("" + conn.getResponseCode());
        conn.close();
    }
}

EDIT

The same code, when I try it with a string, works fine and sends the string to the server. But it is still a problem with the image bytes.

Upvotes: 1

Views: 2239

Answers (4)

Trevor Bernard
Trevor Bernard

Reputation: 992

conn = new HttpConnection(Connector.open(myURL));

This line is wrong. Connection is a factory class that creates new Connection objects by looking it's appropriate protocol implementation.

HttpConnection conn = (HttpConnection) Connector.open(myURL);

The rest of the code seems ok. When you're POSTing, at minimun you need to define the content-type and content-length.

Upvotes: 2

DaveInMaine
DaveInMaine

Reputation:

You'll need to encode the bytes (preferably Base-64) and send that string. The raw bytes aren't likely to be http safe.

Then on the server, you'll need to decode them back into a byte array and do whatever you were going to do with it (save as file, stuff into database, etc.)

Upvotes: 0

Ed Marty
Ed Marty

Reputation: 39690

You most definitely need to set all headers before sending the POST data, including the Content-Length header.

Also, make sure you are sending headers valid for requests, and not response-only headers.

Upvotes: 1

michael aubert
michael aubert

Reputation: 6826

A few things that may be missing from your list:

  • use HttpConnection.setRequestMethod(HttpConnection.POST) between 2 and 3.
  • set content length with HttpConnection.setRequestProperty("Content-Length",...) between 5 and 6.
  • knowing the HTTP request response code can help debug your issues: call HttpConnection.getResponseCode() after you've closed the OutputStream but before you close the HttpConnection.

Upvotes: 4

Related Questions