我正在尝试发送一个简单的字符串作为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);
[...]
Run Code Online (Sandbox Code Playgroud)
字符串应该是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
Run Code Online (Sandbox Code Playgroud)
这应该是显示出来的(在C#中编写了一个非常简单的控制台应用程序来执行此操作,它只是起作用):
+POST /resource HTTP/1.1
+Content-Type: text/plain
+Host: 1.2.3.4
+Content-Length: 11
+Expect: 100-continue …
Run Code Online (Sandbox Code Playgroud) 因为我是android的新手请给我链接教程在服务器上发布xml数据.我面临的问题是执行post请求
public void uploadFileToServer()
{
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url_context + "/orders/order");
httppost.addHeader("Accept", "text/xml");
httppost.addHeader("Content-Type", "application/xml");
try
{
StringEntity entity = new StringEntity(xmlString, "UTF-8");
entity.setContentType("application/xml");
httppost.setEntity(entity);
HttpResponse response = httpClient.execute(httppost);
BasicResponseHandler responseHandler = new BasicResponseHandler();
String strResponse = null;
if (response != null)
{
try {
strResponse = responseHandler.handleResponse(response);
} catch (HttpResponseException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud) 可能重复:
如何在Android中执行HTTP发布?
我想将这两个值发送到服务器,比如http://localhost/twoVal.php,它将进一步处理信息.
例如,
我有val1 = 12和val2 = 13我想将这些数据发布到上面提到的URL.(可能是某些URL不一定是localhost)怎么做?此外,数据需要作为HTTP标头(POST)的一部分发送,而不是作为查询参数发送.