Reputation: 125
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(); }} }
Upvotes: 3
Views: 1741
Reputation: 76898
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");
Upvotes: 4