Android HTTP POST 内容长度异常
我有一个发送 HTTP POST 的简单程序。
它仅在httpost.addheader("Content-Length","18")
行不存在的情况下起作用。否则就会失败。在代码中,是带有“--->”的行
注释掉这一行将使 POST 成功。
如果该行位于代码中,Android 不会发送 POST,并返回协议异常错误。我使用 Wireshark 来验证没有发送任何内容。
任何想法为什么设置内容长度会产生异常?
代码:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://test.com/a_post.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
// DATA
nameValuePairs.add(new BasicNameValuePair("mydata", "abcde"));
nameValuePairs.add(new BasicNameValuePair("id","29"));
StringEntity se = new UrlEncodedFormEntity(nameValuePairs);
httppost.setEntity(se);
int seLength = (int) se.getContentLength();
String seLengthStr = Integer.toString(seLength);
httppost.addHeader("Content-Type","application/x-www-form-urlencoded");
----> httppost.addHeader("Content-Length", "18");
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String httpResponse=httpclient.execute(httppost, responseHandler);
responseV.setText(responseV.getText()+ " " + httpResponse);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I have a simple program that sends an HTTP POST.
It works only if the line httpost.addheader("Content-Length","18")
is not present. Otherwise it fails. In the code, it's the line with "--->"
Commenting this line out makes the POST succeed.
Android does not send the POST if that line is in the code and returns with a Protocol Exception error. I used Wireshark to verify that nothing is sent.
Any ideas why setting the Content-Length generates an exception?
The code:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://test.com/a_post.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
// DATA
nameValuePairs.add(new BasicNameValuePair("mydata", "abcde"));
nameValuePairs.add(new BasicNameValuePair("id","29"));
StringEntity se = new UrlEncodedFormEntity(nameValuePairs);
httppost.setEntity(se);
int seLength = (int) se.getContentLength();
String seLengthStr = Integer.toString(seLength);
httppost.addHeader("Content-Type","application/x-www-form-urlencoded");
----> httppost.addHeader("Content-Length", "18");
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String httpResponse=httpclient.execute(httppost, responseHandler);
responseV.setText(responseV.getText()+ " " + httpResponse);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您确定内容长度正好是 18 吗?我的猜测是,如果代码意识到设置的内容长度不正确,则请求不会完成,因为发送具有无效内容长度的请求将(至少应该)导致服务器上的错误。
如果您省略内容长度,很可能会在需要时自动添加。
Are you sure the content-length is exactly 18? My guess is that the request is not done if the code realizes that the set content-length is incorrect, as sending a request with an invalid content-length will (at least should) cause an error on the server.
Most likely if you omit the content-length, it is automatically added when required.
在许多情况下,HTTP 客户端和服务器不得发送
Content-Length
标头。通常不需要,包括连接的另一端支持 HTTP/1.1 的情况。最好简单地保留该标头,并让您的 HTTP 客户端/服务器库处理是否添加标头的逻辑。There are a number of conditions where HTTP clients and servers must not send the
Content-Length
header. It is often not required, including if the other side of the connection is HTTP/1.1-aware. It may be best simply to leave that header out, and let your HTTP client/server library handle the logic of whether to add the header.