Java httpPost未正确发布数据(Apache库)?
快速提问,因为我尝试了许多调试选项,但没有一个能够帮助识别问题。我正在使用Java和ApacheHttpPost库 目前我的httpGet和httpHead工作正常,但是我无法让httpPost正确发送变量。我在服务器上设置了一个调试PHP脚本,只需转储Java httpPost未正确发布数据(Apache库)?,java,apache,entity,http-post,Java,Apache,Entity,Http Post,快速提问,因为我尝试了许多调试选项,但没有一个能够帮助识别问题。我正在使用Java和ApacheHttpPost库 目前我的httpGet和httpHead工作正常,但是我无法让httpPost正确发送变量。我在服务器上设置了一个调试PHP脚本,只需转储$\u POST的内容。如果在终端中执行curl请求,则工作正常 代码段: // create new HttpPost object postRequest = new HttpPost(url); // add po
$\u POST
的内容。如果在终端中执行curl请求,则工作正常
代码段:
// create new HttpPost object
postRequest = new HttpPost(url);
// add post variables
postRequest.setEntity(new StringEntity(body, ContentType.TEXT_HTML));
// execute request
response = executeRequest(postRequest);
字符串变量“body”只是一个字符串,当前包含“test=testing”。此PHP调试脚本的输出:
<?php
echo "Input Stream: ";
var_dump(file_get_contents('php://input'));
echo "POST Variables: ";
var_dump($_POST);
?>
有人知道为什么它没有被提取并转储到$\u POST
变量中吗?非常恼人,因为我已经花了好几个小时在这上面了
任何帮助都将不胜感激:)谢谢
要构建包含参数的URI,请使用:
NameValuePair nvp = new BasicNameValuePair("test", "testing");
String query = URLEncodedUtils.format(nvp, "utf-8"); // use your favourite charset here
URI uri = new URI("http://localhost/app?" + query;
HttpPost postReq = new HttpPost(uri);
httpClient.execute(postReq);
这将导致:
http://localhost/app?test=testing
被请求。当你说“Apache HttpPost”库时,你的意思是?@david是的,我在使用那些库executeRequest方法做什么?@Alex执行HttpClient执行方法并返回httpResponse对象,随后检查是否有效响应(200),然后打印响应实体
NameValuePair nvp = new BasicNameValuePair("test", "testing");
String query = URLEncodedUtils.format(nvp, "utf-8"); // use your favourite charset here
URI uri = new URI("http://localhost/app?" + query;
HttpPost postReq = new HttpPost(uri);
httpClient.execute(postReq);