【发布时间】:2025-12-25 03:15:07
【问题描述】:
我正在尝试发送一个简单的字符串作为 HttpPost 消息的内容。
问题是,HttpPost 消息的正文永远不会到达网络。 (说 Wireshark 捕获)。标头看起来还不错(包括正确计算的 Content-Length。
代码如下:
String url = "http://1.2.3.4/resource";
HttpClient client = new DefaultHttpClient();
String cmd = "AT+AVLPOS\r\n";
StringEntity se = new StringEntity(cmd);
se.setContentType("text/plain");
HttpPost request = new HttpPost(url);
request.setHeader("Content-Type","text/plain");
request.setEntity(se);
HttpResponse response = client.execute(request);
[...]
字符串应该是 ASCII 编码的,但这是一个细节。
这是 WireShark 中显示的内容: -> 请注意,标有 + 的行是发送的,而 - 是接收的。
+POST /resource HTTP/1.1
+Content-Type: text/plain
+Content-Length: 11
+Host: 1.2.3.4
+Connection: Keep-Alive
+User-Agent: Apache-HttpClient/UNAVAILABLE (java 1.4)
+Expect: 100-Continue
-HTTP/1.1 200 OK
-Content-Type: text/plain
-Transfer-Encoding: chunked
-4
-OK
这是应该显示的(用 C# 编写了一个非常简单的控制台应用程序来执行此操作,它可以正常工作):
+POST /resource HTTP/1.1
+Content-Type: text/plain
+Host: 1.2.3.4
+Content-Length: 11
+Expect: 100-continue
+Connection: Keep-Alive
+
-HTTP/1.1 200 OK
-Content-Type: text/plain
-Transfer-Encoding: chunked
-
+AT+AVLPOS
+
-4
-OK
-
-48
-$AVTMR,99999999,204810,A,1234.2218,N,0123.1051,E,0,20,150811,0,REQ*69
-
-0
-
有什么建议吗?
【问题讨论】:
-
This 应该回答你的问题:)
-
@flob 我想我已经阅读了所有与 Android Http Post 相关的 SO 问题,包括那个问题 - 这就是我走到这一步的方式,但它仍然不起作用。
标签: java android networking http-post