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?
将表单数据发布到Web服务器时遇到问题。每次我尝试发布我从服务器“没有收到消息”得到此响应,所以显然代码有问题。我错过了什么吗?
Here is the HTML part that i am trying to post:
这是我要发布的HTML部分:
<form id="post" action="url" method="post" enctype="multipart/form-data">
<fieldset>
<input id="board" name="board" value="hiekkalaatikko" type="hidden">
<input id="thread" name="thread" value="0" type="hidden">
<input name="uuid" id="uuid" value="1e54a561-b08e-4bad-b9b6-e618ccd91ef5" type="hidden">
<input name="email" style="position: absolute; left: -9999px;" type="text">
<table id="postform">
<tbody>
<tr>
<td class="label"><label for="postername">Viestinimi</label></td>
<td><input name="postername" id="postername" onkeyup="checkName('poster');" type="text"> <span id="posternamestatus"></span></td>
</tr>
<tr>
<td class="label"><label for="functions">Toiminnot</label></td>
<td><input name="functions" id="functions" type="text"></td>
</tr>
<tr>
<td class="label"><label for="subject">Aihe</label></td>
<td>
<input name="subject" id="subject" maxlength="60" type="text">
<input value="Lähetä" name="submit" id="submit" type="submit">
<span id="qrinfo"></span>
</td>
</tr>
<tr>
<td class="label"><label for="msg">Viesti</label></td>
<td><textarea name="msg" id="msg" rows="4" cols="48"></textarea></td>
</tr>
<tr>
<td class="label"><label for="file">Tiedosto</label></td>
<td><input name="file" id="file" size="35" type="file"> <label for="spoilerfile">Juonipaljastus</label> <input id="spoilerfile" name="spoilerfile" type="checkbox"></td>
</tr>
<tr>
<td class="label"><label for="embed">Upote</label></td>
<td>
<input name="embed" id="embed" type="text">
<select name="embedtype" id="embedtype">
<option value="9">Naamapalmu</option>
<option value="1" selected="selected">YouTube</option>
<option value="7">LiveLeak</option>
<option value="4">SoundCloud</option>
<option value="8">Vimeo</option>
<option value="5">Vocaroo</option>
</select> <a href="http://ylilauta.org/scripts/help.php?embeds" onclick="window.open(this.href,'embedhelp','width=640,height=480,scrollbars=yes'); return false;">Upotusohje</a>
</td>
</tr>
<tr>
<td colspan="2">
<ul id="postinfo">
<li>Sallitut tiedostotyypit ovat gif, jpeg, jpg, mp3, png, rar, swf, zip</li>
<li>Suurin sallittu tiedostokoko on 10 Mt.</li>
<li>2642 käyttäjää paikalla. (<a href="http://ylilauta.org/online.php">Graafi</a>)</li>
<li>Yhteensä 10161 viestiä on lähetetty tälle alueelle. (<a href="http://ylilauta.org/postcount.php">Graafi</a>)</li>
<li><a href="http://ylilauta.org/hiekkalaatikko/threadlist">Aiheluettelo</a></li>
</ul>
</td>
</tr>
</tbody>
</table>
</fieldset>
</form>
Here is android side code:
这是android端代码:
@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<MultipartEntity, Void, HttpResponse>{
@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;
}
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.
编辑:我尝试从浏览器和此应用程序将此数据发送到测试服务器。我没有看到任何数据丢失,但我仍然从目标服务器得到同样的响应“没有收到消息”。
This is send from the browser:
这是从浏览器发送的:
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.
And this is from my application:
这是我的申请:
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)
4 个解决方案
#1
2
You must try to encode your value by changing your MultipartEntry with. It can also help you repeating Charset.forName("UTF-8")
everywhere
您必须尝试通过更改MultipartEntry来编码您的值。它还可以帮助您在任何地方重复Charset.forName(“UTF-8”)
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, null, Charset.forName(HTTP.UTF_8));
Dont forget also to add this
别忘了也加上这个
httpclient.getParams()
.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
add this line before execute to see your request line that you are sending to the server
在执行之前添加此行以查看您要发送到服务器的请求行
System.out.println("executing request " + httppost.getRequestLine());
and this also for to see response line from server
这也是为了从服务器看到响应行
response.getStatusLine().toString()
add also this line to close the connection after httpresponse
添加此行以在httpresponse之后关闭连接
httpclient.getConnectionManager().shutdown();
#2
4
You're sending a barrage of entities rather than the UrlEncoded
entity the server expects. Try following this pattern:
您发送的是一系列实体,而不是服务器期望的UrlEncoded实体。请尝试以下模式:
List<NameValuePair> values = new ArrayList<NameValuePair>();
values.add( new BasicNameValuePair( "board", "hiekkalaatikko" ) );
values.add( new BasicNameValuePair( "thread", "0" ) );
...
httppost.setEntity( new UrlEncodedFormEntity( values ) );
You won't really need multi-part entities unless you're sending files to the server.
除非您将文件发送到服务器,否则您不会真正需要多部分实体。
#3
1
Use this method ::
使用此方法::
public static String uploadFile2Server(String uploadUrl, String filePath,
String serverKeyName) {
HttpURLConnection urlConnection = null;
DataOutputStream dos = null;
DataInputStream inputStream = null;
String lineEnd = "rn";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
StringBuilder stringBuilder = null;
try {
// ------------------ CLIENT REQUEST
FileInputStream fileInputStream = new FileInputStream(new File(
filePath));
// open a URL connection to the Server
URL url = new URL(uploadUrl);
// Open a HTTP connection to the URL
urlConnection = (HttpURLConnection) url.openConnection();
// Allow Inputs
urlConnection.setDoInput(true);
// Allow Outputs
urlConnection.setDoOutput(true);
// Don't use a cached copy.
urlConnection.setUseCaches(false);
// Use a post method.
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Connection", "Keep-Alive");
urlConnection.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);
dos = new DataOutputStream(urlConnection.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\""
+ serverKeyName + "\";filename=\"" + filePath + " \""
+ lineEnd);
dos.writeBytes(lineEnd);
// create a buffer of maximum size
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// read file and write it into form...
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
// send multipart form data necesssary after file data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// close streams
fileInputStream.close();
dos.flush();
dos.close();
} catch (Exception exception) {
exception.printStackTrace();
}
// ------------------ read the SERVER RESPONSE
try {
inputStream = new DataInputStream(urlConnection.getInputStream());
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(inputStream));
stringBuilder = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line);
}
bufferedReader.close();
inputStream.close();
} catch (Exception exception) {
exception.printStackTrace();
}
return stringBuilder.toString();
}
#4
0
Don't forget to set a boundary in the constructor of your MultipartEntity object in case you are defining one server side.
如果要定义一个服务器端,请不要忘记在MultipartEntity对象的构造函数中设置边界。
// MultipartEntity(HttpMultipartMode mode, String boundary, Charset charset)
MultipartEntity entity = new MultipartEntity(null, "your boundary", null);
*Extra info about boundaries: http://en.wikipedia.org/wiki/MIME#Multipart_messages
*关于边界的额外信息:http://en.wikipedia.org/wiki/MIME#Multipart_messages
#1
2
You must try to encode your value by changing your MultipartEntry with. It can also help you repeating Charset.forName("UTF-8")
everywhere
您必须尝试通过更改MultipartEntry来编码您的值。它还可以帮助您在任何地方重复Charset.forName(“UTF-8”)
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, null, Charset.forName(HTTP.UTF_8));
Dont forget also to add this
别忘了也加上这个
httpclient.getParams()
.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
add this line before execute to see your request line that you are sending to the server
在执行之前添加此行以查看您要发送到服务器的请求行
System.out.println("executing request " + httppost.getRequestLine());
and this also for to see response line from server
这也是为了从服务器看到响应行
response.getStatusLine().toString()
add also this line to close the connection after httpresponse
添加此行以在httpresponse之后关闭连接
httpclient.getConnectionManager().shutdown();
#2
4
You're sending a barrage of entities rather than the UrlEncoded
entity the server expects. Try following this pattern:
您发送的是一系列实体,而不是服务器期望的UrlEncoded实体。请尝试以下模式:
List<NameValuePair> values = new ArrayList<NameValuePair>();
values.add( new BasicNameValuePair( "board", "hiekkalaatikko" ) );
values.add( new BasicNameValuePair( "thread", "0" ) );
...
httppost.setEntity( new UrlEncodedFormEntity( values ) );
You won't really need multi-part entities unless you're sending files to the server.
除非您将文件发送到服务器,否则您不会真正需要多部分实体。
#3
1
Use this method ::
使用此方法::
public static String uploadFile2Server(String uploadUrl, String filePath,
String serverKeyName) {
HttpURLConnection urlConnection = null;
DataOutputStream dos = null;
DataInputStream inputStream = null;
String lineEnd = "rn";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
StringBuilder stringBuilder = null;
try {
// ------------------ CLIENT REQUEST
FileInputStream fileInputStream = new FileInputStream(new File(
filePath));
// open a URL connection to the Server
URL url = new URL(uploadUrl);
// Open a HTTP connection to the URL
urlConnection = (HttpURLConnection) url.openConnection();
// Allow Inputs
urlConnection.setDoInput(true);
// Allow Outputs
urlConnection.setDoOutput(true);
// Don't use a cached copy.
urlConnection.setUseCaches(false);
// Use a post method.
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Connection", "Keep-Alive");
urlConnection.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);
dos = new DataOutputStream(urlConnection.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\""
+ serverKeyName + "\";filename=\"" + filePath + " \""
+ lineEnd);
dos.writeBytes(lineEnd);
// create a buffer of maximum size
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// read file and write it into form...
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
// send multipart form data necesssary after file data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// close streams
fileInputStream.close();
dos.flush();
dos.close();
} catch (Exception exception) {
exception.printStackTrace();
}
// ------------------ read the SERVER RESPONSE
try {
inputStream = new DataInputStream(urlConnection.getInputStream());
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(inputStream));
stringBuilder = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line);
}
bufferedReader.close();
inputStream.close();
} catch (Exception exception) {
exception.printStackTrace();
}
return stringBuilder.toString();
}
#4
0
Don't forget to set a boundary in the constructor of your MultipartEntity object in case you are defining one server side.
如果要定义一个服务器端,请不要忘记在MultipartEntity对象的构造函数中设置边界。
// MultipartEntity(HttpMultipartMode mode, String boundary, Charset charset)
MultipartEntity entity = new MultipartEntity(null, "your boundary", null);
*Extra info about boundaries: http://en.wikipedia.org/wiki/MIME#Multipart_messages
*关于边界的额外信息:http://en.wikipedia.org/wiki/MIME#Multipart_messages