1

快速提问,因为我尝试了许多调试选项,但都没有帮助确定问题。我正在使用 Java 和 Apache HttpPost 库。

我目前的 httpGet 和 httpHead 工作正常,但是我无法让 httpPost 正确发送变量。我在我的服务器上设置了一个调试 PHP 脚本来简单地转储$_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);

?>

是 :

Input Stream: string(12) "test=testing"
POST Variables: array(0) {
}

有谁知道为什么它没有被拾取并转储到$_POST变量中?非常烦人,因为我花了几个小时在这上面!

任何帮助将不胜感激:)谢谢

4

1 回答 1

0

请参阅访问 HttpParams 的所有条目

要构建包含参数的 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被请求。

于 2013-04-25T10:17:07.007 回答