HttpPost查看post params

时间:2016-02-19 22:35:13

标签: java apache-httpclient-4.x

我希望能够使用params查看完整的HttpClient / HttpPost URI。我不知道如何将它输出到我的控制台。

HttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost(url);
httppost.setHeader("ContentType","application/x-www-form-urlencoded");
httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
System.out.println(httppost.getURI());

打印getURI仅输出baseURI而不是参数。

有人可以帮助我解决我失踪的问题吗?

1 个答案:

答案 0 :(得分:1)

实体有一个将自身序列化为输出流writeTo()的方法,因此您可以创建一个并将实体转储到那里。这不是整个请求,只有UrlEncodedFormEntity公开编码参数:

ByteArrayOutputStream outs = new ByteArrayOutputStream();
httppost.getEntity().writeTo(outs);
System.out.println(outs.toString("UTF-8"));
outs.close();

结果是这样的一行:

foul=play&foo=bar&baz=bam