Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android HttpPost消息赢得';Don’不要通过电线发送有效载荷_Java_Android_Networking_Http Post - Fatal编程技术网

Java Android HttpPost消息赢得';Don’不要通过电线发送有效载荷

Java Android HttpPost消息赢得';Don’不要通过电线发送有效载荷,java,android,networking,http-post,Java,Android,Networking,Http Post,我试图发送一个简单的字符串作为HttpPost消息的内容 问题是,HttpPost消息的主体永远不会到达电线。(捕捉鲨鱼协会说)。但标题看起来很好(包括正确计算的内容长度) 下面是代码的样子: String url = "http://1.2.3.4/resource"; HttpClient client = new DefaultHttpClient(); String cmd = "AT+AVLPOS\r\n"; StringEntity se = new StringEntity(cmd

我试图发送一个简单的字符串作为HttpPost消息的内容

问题是,HttpPost消息的主体永远不会到达电线。(捕捉鲨鱼协会说)。但标题看起来很好(包括正确计算的内容长度)

下面是代码的样子:

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编写了一个非常简单的控制台应用程序#要做到这一点,它只起作用):


有什么建议吗?

我想出来了,今天我学到了一些东西

长话短说:禁用HttpClient的
httppost expect continue
握手,这样将在一个块中发送整个请求消息

//set up HttpPost request as before
HttpClient client = new DefaultHttpClient();
client.getParams().setBooleanParameter("http.protocol.expect-continue", false);
HttpResponse response = client.execute(request);
[...]
这就是我是怎么做到的,也许有一天这会帮助别人

首先,我从a派生并将其用作请求实体,以查看何时调用什么,并发现
实体的
writeTo(OutputStream)
方法从未被调用

然后我开始研究为什么在“正确”行为的情况下,POST请求不是一次发送的,而是发送请求头,然后接收响应头,然后发送请求体

这一切都和
httppost-expect-continue
handshake有关。 如果在请求中发送expect continue标头,Http服务器应回复
100 continue
消息,表示“OK,我将接受您的消息”,或者回复错误,停止跟踪可能较长的POST消息

不幸的是,我运行的web服务器是一个在芯片上运行的简单实现,它发送了错误的回复(
200ok
,而不是
100continue
)。
在这里,.NET Http客户端的默认实现似乎更宽容:它将
200
消息视为
100 Continue
,耸耸肩,开始发送请求正文

Android的Http客户端实现(API级别7)并非如此


接下来我尝试的是完全禁用
expect continue
握手,以使HttpClient发送整个请求。令我惊讶和高兴的是,web服务器处理得很好,它回答了我想要的信息。耶!

应该回答你的问题:)@flob我想我已经阅读了所有关于Android Http Post的问题,包括那篇——我就是这样走到这一步的,但它仍然不起作用。谢谢!!我有一个帖子挂在客户机上,不管我怎么做都要执行。你救了这一天。
//set up HttpPost request as before
HttpClient client = new DefaultHttpClient();
client.getParams().setBooleanParameter("http.protocol.expect-continue", false);
HttpResponse response = client.execute(request);
[...]