使用 Apache http 查询参数 post

Query parameters with Apache http post

我正在尝试通过 Apache http post 访问 API,其中 returns Json 包含查询参数。端点在浏览器上运行良好,但我的 post 请求完全忽略了查询参数。它在浏览器上完全正常工作。

HttpPost post = new HttpPost(url);// http://hostname:port/type/endpoint1
post.addHeader("content-type","text/plain");
post.addHeader("Accept","text/plain");
post.setEntity(new StringEntity("?key=value"));
response = client.execute(post);

此 returns 仅针对 url 的回复。并且完全忽略参数。

将您的 content-type 更改为 application/x-www-form-urlencoded,并从正文中删除前导 ?