Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to send multipart/form data to web server from android?
    text
    copied!<p>I have a trouble with posting the form data to web server. Everytime i try to post i get this response from server "No message received" so obviously there is something wrong with the code. Am i missing something?</p> <p>Here is the HTML part that i am trying to post:</p> <pre><code>&lt;form id="post" action="url" method="post" enctype="multipart/form-data"&gt; &lt;fieldset&gt; &lt;input id="board" name="board" value="hiekkalaatikko" type="hidden"&gt; &lt;input id="thread" name="thread" value="0" type="hidden"&gt; &lt;input name="uuid" id="uuid" value="1e54a561-b08e-4bad-b9b6-e618ccd91ef5" type="hidden"&gt; &lt;input name="email" style="position: absolute; left: -9999px;" type="text"&gt; &lt;table id="postform"&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td class="label"&gt;&lt;label for="postername"&gt;Viestinimi&lt;/label&gt;&lt;/td&gt; &lt;td&gt;&lt;input name="postername" id="postername" onkeyup="checkName('poster');" type="text"&gt; &lt;span id="posternamestatus"&gt;&lt;/span&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class="label"&gt;&lt;label for="functions"&gt;Toiminnot&lt;/label&gt;&lt;/td&gt; &lt;td&gt;&lt;input name="functions" id="functions" type="text"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class="label"&gt;&lt;label for="subject"&gt;Aihe&lt;/label&gt;&lt;/td&gt; &lt;td&gt; &lt;input name="subject" id="subject" maxlength="60" type="text"&gt; &lt;input value="Lähetä" name="submit" id="submit" type="submit"&gt; &lt;span id="qrinfo"&gt;&lt;/span&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class="label"&gt;&lt;label for="msg"&gt;Viesti&lt;/label&gt;&lt;/td&gt; &lt;td&gt;&lt;textarea name="msg" id="msg" rows="4" cols="48"&gt;&lt;/textarea&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class="label"&gt;&lt;label for="file"&gt;Tiedosto&lt;/label&gt;&lt;/td&gt; &lt;td&gt;&lt;input name="file" id="file" size="35" type="file"&gt; &lt;label for="spoilerfile"&gt;Juonipaljastus&lt;/label&gt; &lt;input id="spoilerfile" name="spoilerfile" type="checkbox"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class="label"&gt;&lt;label for="embed"&gt;Upote&lt;/label&gt;&lt;/td&gt; &lt;td&gt; &lt;input name="embed" id="embed" type="text"&gt; &lt;select name="embedtype" id="embedtype"&gt; &lt;option value="9"&gt;Naamapalmu&lt;/option&gt; &lt;option value="1" selected="selected"&gt;YouTube&lt;/option&gt; &lt;option value="7"&gt;LiveLeak&lt;/option&gt; &lt;option value="4"&gt;SoundCloud&lt;/option&gt; &lt;option value="8"&gt;Vimeo&lt;/option&gt; &lt;option value="5"&gt;Vocaroo&lt;/option&gt; &lt;/select&gt; &lt;a href="http://ylilauta.org/scripts/help.php?embeds" onclick="window.open(this.href,'embedhelp','width=640,height=480,scrollbars=yes'); return false;"&gt;Upotusohje&lt;/a&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td colspan="2"&gt; &lt;ul id="postinfo"&gt; &lt;li&gt;Sallitut tiedostotyypit ovat gif, jpeg, jpg, mp3, png, rar, swf, zip&lt;/li&gt; &lt;li&gt;Suurin sallittu tiedostokoko on 10 Mt.&lt;/li&gt; &lt;li&gt;2642 käyttäjää paikalla. (&lt;a href="http://ylilauta.org/online.php"&gt;Graafi&lt;/a&gt;)&lt;/li&gt; &lt;li&gt;Yhteensä 10161 viestiä on lähetetty tälle alueelle. (&lt;a href="http://ylilauta.org/postcount.php"&gt;Graafi&lt;/a&gt;)&lt;/li&gt; &lt;li&gt;&lt;a href="http://ylilauta.org/hiekkalaatikko/threadlist"&gt;Aiheluettelo&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;/fieldset&gt; &lt;/form&gt; </code></pre> <p>Here is android side code:</p> <pre><code>@Override public void onClick(View v) { switch(v.getId()){ case R.id.send: MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); try{ reqEntity.addPart("content-type", new StringBody("text/html; charset=UTF-8")); reqEntity.addPart("board", new StringBody("hiekkalaatikko",Charset.forName("UTF-8"))); reqEntity.addPart("thread", new StringBody("0", Charset.forName("UTF-8"))); reqEntity.addPart("anticaptcha", new StringBody(anticaptcha, Charset.forName("UTF-8"))); reqEntity.addPart("uuid", new StringBody(UUID, Charset.forName("UTF-8"))); reqEntity.addPart("email", new StringBody("", Charset.forName("UTF-8"))); reqEntity.addPart("postername", new StringBody(mNameField.getText().toString(), Charset.forName("UTF-8"))); reqEntity.addPart("functions", new StringBody(mActionField.getText().toString(), Charset.forName("UTF-8"))); reqEntity.addPart("subject", new StringBody(mSubjectField.getText().toString(),Charset.forName("UTF-8"))); reqEntity.addPart("submit", new StringBody("Lähetä", Charset.forName("UTF-8"))); reqEntity.addPart("subboard", new StringBody("0", Charset.forName("UTF-8"))); reqEntity.addPart("msg", new StringBody(mMessageField.getText().toString(), Charset.forName("UTF-8"))); reqEntity.addPart("spoilerfile", new StringBody("checked", Charset.forName("UTF-8"))); reqEntity.addPart("embed", new StringBody("9", Charset.forName("UTF-8"))); } catch(Exception e){} postReply reply = new postReply(); reply.execute(reqEntity); break; } } public class postReply extends AsyncTask&lt;MultipartEntity, Void, HttpResponse&gt;{ @Override protected HttpResponse doInBackground(MultipartEntity... arg0) { array = arg0[0]; HttpResponse response = null; try { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("url"); httppost.setEntity(arg0[0]); response = httpclient.execute(httppost); BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8")); String sResponse; StringBuilder s = new StringBuilder(); while ((sResponse = reader.readLine()) != null) { s = s.append(sResponse); } System.out.println(s); } catch (IOException e) { Log.i("Post", "Exception"); e.printStackTrace(); } return response; } </code></pre> <p>Edit: I tryed to send this data to test server from browser and this application. I don't see any data missing, but i get still this same response "No message received" from the target server.</p> <p>This is send from the browser:</p> <pre><code> Time: Wed, 10 Apr 13 11:33:30 -0700 Source ip: *********** Headers (Some may be inserted by server) UNIQUE_ID = UWWwetBx6hIAAA7wUFQAAAAK HTTP_HOST = posttestserver.com HTTP_USER_AGENT = Mozilla/5.0 (Windows NT 6.1; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0 HTTP_ACCEPT = text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 HTTP_ACCEPT_LANGUAGE = fi-fi,fi;q=0.8,en-us;q=0.5,en;q=0.3 HTTP_ACCEPT_ENCODING = gzip, deflate HTTP_CONNECTION = close CONTENT_TYPE = multipart/form-data; boundary=---------------------------1974624714324 CONTENT_LENGTH = 1326 REMOTE_ADDR = ************** REMOTE_PORT = 11440 GATEWAY_INTERFACE = CGI/1.1 REQUEST_METHOD = POST QUERY_STRING = dir=example REQUEST_URI = /post.php?dir=example REQUEST_TIME = 1365618810 Post Params: key: 'board' value: 'hiekkalaatikko' key: 'thread' value: '0' key: 'uuid' value: '1e54a561-b08e-4bad-b9b6-e618ccd91ef5' key: 'email' value: '' key: 'postername' value: 'name ' key: 'functions' value: 'functions' key: 'subject' value: '' key: 'submit' value: 'Lähetä' key: 'msg' value: 'Test message' key: 'embed' value: '' key: 'embedtype' value: '1' Empty post body. == Multipart File upload. == Received 1 file(s) 0: posted name=file name: type: error: 4 size: 0 File specified was not uploaded. Possible file upload attack. </code></pre> <p>And this is from my application:</p> <pre><code> Time: Wed, 10 Apr 13 11:35:01 -0700 Source ************* Headers (Some may be inserted by server) UNIQUE_ID = UWWw1dBx6hIAAA7bTL8AAAAD HTTP_HTTP_ACCEPT = text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 HTTP_HTTP_ACCEPT_LANGUAGE = fi-fi,fi;q=0.8,en-us;q=0.5,en;q=0.3 CONTENT_LENGTH = 2189 CONTENT_TYPE = multipart/form-data; boundary=---------------------------321842129511553 HTTP_HOST = posttestserver.com HTTP_CONNECTION = close HTTP_USER_AGENT = Mozilla/5.0 (Windows NT 6.1; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0 REMOTE_ADDR = *************** REMOTE_PORT = 11448 GATEWAY_INTERFACE = CGI/1.1 REQUEST_METHOD = POST QUERY_STRING = dir=example REQUEST_URI = /post.php?dir=example REQUEST_TIME = 1365618901 Post Params: key: 'board' value: 'hiekkalaatikko' key: 'thread' value: '0' key: 'uuid' value: '0c44a067-802d-480d-b636-335d6d837c13' key: 'email' value: '' key: 'subject' value: '' key: 'embed' value: '' key: 'embedtype' value: '9' key: 'msg' value: 'Test message' key: 'submit' value: 'Luo aihe' key: 'postername' value: 'Name' key: 'functions' value: 'Functions' key: 'noko' value: 'off' Empty post body. == Multipart File upload. == Received 0 file(s) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload