On Fri, 2012-11-16 at 00:05 +0800, Zhan Zhifeng wrote:
> hi, Oleg
>
> How can I "_always_ response content "?
>
> I have do these below:
> HttpEntity entity = response.getEntity();
> if (entity != null && header_[0].getValue().
> contains("text/html")) {
> String html_ = EntityUtils.toString(entity, "UTF-8");
> EntityUtils.consume(entity);
> } else {
> EntityUtils.consume(entity);
> }
>
>
Option 1
----------------------------
HttpGet httpGet = new HttpGet("http://targethost/stuff");
ResponseHandler<T> responseHandler = new MyResponseHandler();
// Connection will be released automatically
T result = httpclient.execute(httpGet, responseHandler);
----------------------------
Option 2
----------------------------
HttpGet httpGet = new HttpGet("http://targethost/stuff");
HttpResponse response = httpclient.execute(httpGet);
try {
HttpEntity entity = response1.getEntity();
// do something useful with the response body
// and manually ensure it is fully consumed
} finally {
EntityUtils.consume(response.getEntity());
}
----------------------------
Option 3 (HttpClient 3.x style)
----------------------------
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://targethost/homepage");
HttpResponse response = httpclient.execute(httpGet);
try {
System.out.println(response1.getStatusLine());
HttpEntity entity = response1.getEntity();
// do something useful with the response body
} finally {
httpGet.releaseConnection();
}
----------------------------
Oleg
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]