gpt4 book ai didi

Java Apache HttpComponents,如何读取 POST 响应?

转载 作者:行者123 更新时间:2023-12-04 05:17:22 28 4
gpt4 key购买 nike

我在 Java 中使用 HttpComponents 将登录信息发布到网站。登录后,我想发送更多的 POST 数据,具体取决于返回的网页。我不确定如何实际打印或查看服务器因发送我的 POST 数据而返回的 html/网页(也许我正在寻找的是响应正文?)。

所有的教程似乎只展示了如何查看标题信息或服务器代码。我认为这可能是我所缺少的简单东西。可能是我对这个过程不是很了解。

到目前为止我的代码:

public class httpposter {
public static void main(String[] args) throws Exception {
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://hroch486.icpf.cas.cz/cgi-bin/echo.pl");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("username", "vip"));
nvps.add(new BasicNameValuePair("password", "secret"));
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
HttpResponse response2 = httpclient.execute(httpPost);
try {
System.out.println(response2);
HttpEntity entity2 = response2.getEntity();
// do something useful with the response body
// and ensure it is fully consumed
EntityUtils.consume(entity2);
} finally {
httpPost.releaseConnection(); }} }

最佳答案

我猜你复制并粘贴了这段代码?

HttpEntity entity2 = response2.getEntity();
// do something useful with the response body
// and ensure it is fully consumed
EntityUtils.consume(entity2);

Javadocs 是你的 friend :HttpEntity Javadocs

IOUtils 使这更容易:

String body = IOUtils.toString(entity2.getContent(), "UTF-8");

关于Java Apache HttpComponents,如何读取 POST 响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14093698/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com