I am using HttpClient Get method to retrieve the response body of the HTTP method, if any.
During the execution, the console print this warning message:
Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.
The code snippet is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | package albert.practice.http; import java.io.IOException; import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler; import org.apache.commons.httpclient.HostConfiguration; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.params.HttpMethodParams; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.google.common.base.Strings; public class HttpSmsClient { private final static Logger logger = LoggerFactory.getLogger(HttpSmsClient.class); public static String getResponse(String url, String proxy, String port) throws IOException { String responseString = ""; HttpClient client = new HttpClient(); if (!Strings.isNullOrEmpty(proxy) && !Strings.isNullOrEmpty(proxy)) { HostConfiguration hostConfiguration = client.getHostConfiguration(); hostConfiguration.setProxy(proxy, Integer.valueOf(port)); client.setHostConfiguration(hostConfiguration); } GetMethod method = new GetMethod(url); method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); int statusCode; try { statusCode = client.executeMethod(method); if (HttpStatus.SC_OK == statusCode) { responseString = method.getResponseBodyAsString(); } } catch (IOException e) { throw e; } finally { method.releaseConnection(); } logger.info("responseString = " + responseString); return responseString; } } |
How-To
This warning message results from Line 38
According to its recommendation, the source code will be modified as bellows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | package albert.practice.http; import java.io.IOException; import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler; import org.apache.commons.httpclient.HostConfiguration; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.params.HttpMethodParams; import org.apache.commons.io.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.google.common.base.Charsets; import com.google.common.base.Strings; public class HttpSmsClient { private final static Logger logger = LoggerFactory.getLogger(HttpSmsClient.class); public static String getResponse(String url, String proxy, String port) throws IOException { String responseString = ""; HttpClient client = new HttpClient(); if (!Strings.isNullOrEmpty(proxy) && !Strings.isNullOrEmpty(proxy)) { HostConfiguration hostConfiguration = client.getHostConfiguration(); hostConfiguration.setProxy(proxy, Integer.valueOf(port)); client.setHostConfiguration(hostConfiguration); } logger.info("url = " + url); GetMethod method = new GetMethod(url); method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); int statusCode; try { statusCode = client.executeMethod(method); logger.info("[getResponse] statusCode = " + statusCode); if (HttpStatus.SC_OK == statusCode) { responseString = IOUtils.toString(method.getResponseBodyAsStream(), Charsets.UTF_8); } } catch (IOException e) { throw e; } finally { method.releaseConnection(); } logger.info("responseString = " + responseString); return responseString; } } |
Maven dependencies are as bellows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <dependencies> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.1.7</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-httpclient/commons-httpclient --> <dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> <version>3.1</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-io/commons-io --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>20.0</version> </dependency> </dependencies> |
No comments:
Post a Comment