2

JavaでHttpComponentsを使用して、ログイン情報をWebサイトにPOSTしています。ログイン後、返されたWebページに応じて、より多くのPOSTデータを送信したいと思います。POSTデータを送信した結果としてサーバーが返すhtml/webページを実際に印刷または表示する方法がわかりません(おそらく、私が探しているのは応答本文ですか?)。

すべてのチュートリアルは、ヘッダー情報またはサーバーコードを表示する方法のみを示しているようです。それはおそらく私が見逃している単純なものだと思います。このプロセスをよく理解していない可能性があります。

これまでの私のコード:

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(); }} }
4

1 に答える 1

3

このコードをコピーして貼り付けたと思いますか?

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

Javadocはあなたの友達です:HttpEntity Javadocs

これIOUtilsにより、これがさらに簡単になります。

String body = IOUtils.toString(entity2.getContent(), "UTF-8");
于 2012-12-30T19:20:25.643 に答える