Android HTTP POST 内容长度异常

发布于 2024-09-25 03:27:01 字数 1412 浏览 0 评论 0原文

我有一个发送 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寻找一个思念的角度 2024-10-02 03:27:01

您确定内容长度正好是 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.

叹倦 2024-10-02 03:27:01

在许多情况下,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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文