Hi,
I am trying to send files through a mobile (incoming by Bluetooth) to an Apache server using HTTP POST. Since the files are sometimes bigger than the mobile's buffer I need to send it "Chunked" or in multiple POSTs.
The problem I've got is that if I try to send it Chunked the mobile adds a "Content-Length" header within the first Chunk and the following Chunks seem to be ommited. If I try to send multiple POSTs the user is prompted to allow the network connection each time which is annoying for them.
My questions then are:
1. How do I avoid the mobile adding the "Content-Length" header?
or
2. How do I send multiple POSTs within the same HTTP Connection.
Thank you very much in advance,
Juan Pison
And this is what is actually being sent (captured with SMSniff):Code:public static void HttpCommunication(){ final String CrLf = "\r\n"; String postChunkHeader = null; String postChunkFoot = null; HttpConnection conn = null; OutputStream os = null; InputStream is = null; try{ // Create the Message that will be posted String headers = ""; headers += "-----------------------------4664151417711" + CrLf; headers += "Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"" + fileName + "\"" + CrLf; headers += "Content-Type: application/octet-stream " + CrLf; headers += CrLf; String foot = ""; foot += CrLf + "-----------------------------4664151417711--" + CrLf; //Setup the HTTP POST conn = (HttpConnection)Connector.open(URL); conn.setRequestMethod(HttpConnection.POST); conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=---------------------------4664151417711"); conn.setRequestProperty("Transfer-Encoding", "chunked"); conn.setRequestProperty("Keep-Alive", "300"); os = conn.openOutputStream(); // Creating the Post's Chunks postChunkHeader = Integer.toHexString(headers.length()) + CrLf; postChunkFoot = CrLf + CrLf; os.write(postChunkHeader.getBytes()); os.write(headers.getBytes()); os.write(postChunkFoot.getBytes()); os.flush(); System.gc(); /***************** SEND THE FILE **************************/ int index = 0; int size = 20480; do{ // Case is the last piece and there arn't 20480B left to send, update the 'size' to send if((index+size)>fileBytes.length) size = fileBytes.length - index; // Creating the Post's Chunks postChunkHeader = Integer.toHexString(size) + CrLf; postChunkFoot = CrLf + CrLf; // Filling the Output Buffer in os.write(postChunkHeader.getBytes()); os.write(fileBytes, index, size); os.write(postChunkFoot.getBytes()); // Flushing the Output Buffer and Sending the data System.gc(); os.flush(); System.gc(); // Updating the 'index' index += size; }while(index<fileBytes.length); /***************** End of SEND THE FILE ********************/ // Creating the Post's Chunks postChunkHeader = Integer.toHexString(foot.length()) + CrLf; postChunkFoot = CrLf + CrLf + "0" + CrLf; os.write(postChunkHeader.getBytes()); os.write(foot.getBytes()); os.write(postChunkFoot.getBytes()); System.gc(); os.flush(); System.gc(); is = conn.openInputStream(); char buff = 2; byte[] data = new byte[buff]; is.read(data); int serverResponseInt = Integer.parseInt(new String(data, 0, 2)); if (serverResponseInt == 11){ listener.emailSent(); } else { listener.emailFailed(null); } }catch(Exception e){ listener.emailFailed(null); }finally{ try{ os.close(); }catch(Exception e){} try{ is.close(); }catch(Exception e){} try{ conn.close(); }catch(Exception e){} } }
POST Testing.php HTTP/1.1
Content-Type: multipart/form-data; boundary=---------------------------4664151417711
Transfer-Encoding: chunked
Accept: */*
Content-Length: 1174
User-Agent: Nokia6300/2.0 (07.21) Profile/MIDP-2.0 Configuration/CLDC-1.1 UNTRUSTED/1.0
x-wap-profile: "http://nds1.nds.nokia.com/uaprof/N6300r100.xml"
Host: 91.84.208.106:8080
Cache-Control: max-age=43200
Connection: keep-alive
48d
-----------------------------4664151417711
Content-Disposition: form-data; name="uploadedfile"; filename="testImage.jpg"
Content-Type: application/octet-stream
[the rest of the file goes next]

Reply With Quote


