3

I am using HttpComponents in Java to POST login information to a website. After I log in, I would like to send more POST data, depending on the webpage returned. I am unsure how to actually print out or view the html/webpage that the server returns as a result of sending my POST data (perhaps what I am looking for is the response body?).

All the tutorials only seem to show how to view header information or server codes. I think it is probably something simple I am missing. It is possible that perhaps I do not understand this process well.

My code so far:

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(); }} }
user1762250
  • 105
  • 1
  • 1
  • 7
  • 1
    Erm ... see the comment that says "Do something useful with the response body" ? http://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/HttpEntity.html – Brian Roach Dec 30 '12 at 19:10
  • `response2.writeTo(OutputStream);` does that even compile? Using `getContent()` is more appropriate in most circumstances. Of course what is in the response depends on the service (and possibly the request headers). – Maarten Bodewes Dec 30 '12 at 19:19
  • Thank you for the quick response! That is exactly what I am trying to do, but I am unsure of how to access it. All the response seems to have is header information. – user1762250 Dec 30 '12 at 19:19
  • @owlstead I am very embarrassed no that part of the code I was playing around with at the last minute. Sorry I removed it. – user1762250 Dec 30 '12 at 19:21
  • @owlstead What do I call getContent() on? HttpResponse doesn't seem to have a method named getContent(). Sorry I am very new to this. – user1762250 Dec 30 '12 at 19:23
  • this is a good reference link https://stackoverflow.com/questions/5769717/how-can-i-get-an-http-response-body-as-a-string – mattsmith5 Jun 14 '22 at 19:22

1 Answers1

4

I'm guessing you copied and pasted this code?

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

Javadocs are your friend: HttpEntity Javadocs

The IOUtils makes this even easier:

String body = IOUtils.toString(entity2.getContent(), "UTF-8");
Brian Roach
  • 76,169
  • 12
  • 136
  • 161
  • Thank you so much. This is my first time trying anything like this and I am little clueless. Yes, it was copied and pasted, and I was trying to figure out how to use it. I did try to read the javadocs before I posted but this was not an obvious solution to me. Thanks again! – user1762250 Dec 30 '12 at 19:39
  • see response here, https://stackoverflow.com/questions/5769717/how-can-i-get-an-http-response-body-as-a-string – mattsmith5 Jun 14 '22 at 19:22