×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    8

    HTTP POST "Chunked" adds "Content-Length"?

    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

    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){}
    		} 
    	}
    And this is what is actually being sent (captured with SMSniff):

    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]

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: HTTP POST "Chunked" adds "Content-Length"?

    I'm afraid you cannot do what you want. As soon as you flush the OutputStream, the connection is made and the complete HTTP transaction performed. You won't be able to send anything larger than the available heap on the device.

    You cannot use the same HttpConnection twice. For another transaction, you need another connection.

    Graham.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    8

    Re: HTTP POST "Chunked" adds "Content-Length"?

    Hi Graham,

    Thank you very much for your response. It's good to have an answer, even if it's not the one I wanted to hear...

    We are running our tests on a Nokia 2630 amongst others that has a specified Heap Size of 600KB. Our JAR is 40KB and using the WTK Emulator Memory Monitor it is using 80KB as it starts the incoming file download. Once the download exceeds 49KB we get an OutOfMemoryError.

    Quick questions now...

    1. Do these numbers make sense to you?

    2. We are intending to get our app signed but haven't done so yet. Will signing it reduce the number of times that the user gets prompted about allowing the HTTP connection?


    Again, thank you very much and happy new year,

    Juan Pison

  4. #4
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: HTTP POST "Chunked" adds "Content-Length"?

    No, these numbers sound wrong. Forget the memory monitor in the WTK for now... how much memory is free on the device? Call System.gc() before calling Runtime.freeMemory() to make sure you're getting the correct figure.

    Signing... depends on whose certificate it's signed with, and on which device the app is running. In many cases, it will simply enable the "always allow" option, but not select it. Nokia publish details for these for vanilla Nokia devices - note that Nokia devices bought from network operators may have different characteristics (especially in the US). This document is a good guide to security domains for Nokias, and there are links to details for specific Nokia platforms.

    Graham.

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    8

    Thumbs up Re: HTTP POST "Chunked" adds "Content-Length"?

    Hi Graham,

    Thank you very much. With your help we improved the memory management and there was no need to change the HTTP POST.

    Thanks again...

    Juan

Similar Threads

  1. waiting for HTTP POST response
    By gemilib in forum Symbian
    Replies: 3
    Last Post: 2007-11-19, 17:00
  2. HTTP POST multipart/form-data
    By flokyboy2006 in forum Symbian Networking & Messaging (Closed)
    Replies: 0
    Last Post: 2006-12-04, 22:35
  3. Using HTTP POST & Nokia 6600 to upload octet stream to server
    By ssandor in forum Mobile Java Networking & Messaging & Security
    Replies: 1
    Last Post: 2005-09-19, 11:00
  4. 7650 J2ME/MIDP HTTP Post
    By cwalkinshaw in forum Mobile Java General
    Replies: 2
    Last Post: 2002-10-23, 10:12
  5. Replies: 0
    Last Post: 2002-05-09, 12:07

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×