下面列出了怎么用org.apache.http.client.entity.GzipDecompressingEntity的API类实例代码及写法,或者点击链接到github查看源代码。
public static CloseableHttpClient buildHttpClient() {
CloseableHttpClient httpClient = HttpClients.custom()
.addInterceptorFirst((HttpRequestInterceptor) (request, context) -> {
if (!request.containsHeader("Accept-Encoding")) {
request.addHeader("Accept-Encoding", "gzip");
}
}).addInterceptorFirst((HttpResponseInterceptor) (response1, context) -> {
HttpEntity entity = response1.getEntity();
if (entity != null) {
Header ceHeader = entity.getContentEncoding();
if (ceHeader != null) {
HeaderElement[] codecs = ceHeader.getElements();
for (HeaderElement codec : codecs) {
if (codec.getName().equalsIgnoreCase("gzip")) {
response1.setEntity(
new GzipDecompressingEntity(response1.getEntity()));
return;
}
}
}
}
}).build();
return httpClient;
}
private static HttpEntity handleCompressedEntity(org.apache.http.HttpEntity entity) {
Header contentEncoding = entity.getContentEncoding();
if (contentEncoding != null)
for (HeaderElement e : contentEncoding.getElements()) {
if (Defaults.CONTENT_ENCODING_GZIP.equalsIgnoreCase(e.getName())) {
return new GzipDecompressingEntity(entity);
}
if (Defaults.CONTENT_ENCODING_DEFLATE.equalsIgnoreCase(e.getName())) {
return new DeflateDecompressingEntity(entity);
}
}
return entity;
}
private void handleContentEncoding(HttpResponse response) throws ServletException {
HttpEntity entity = response.getEntity();
if (entity != null) {
Header contentEncodingHeader = entity.getContentEncoding();
if (contentEncodingHeader != null) {
HeaderElement[] codecs = contentEncodingHeader.getElements();
LOGGER.debug("Content-Encoding in response:");
for (HeaderElement codec : codecs) {
String codecname = codec.getName().toLowerCase();
LOGGER.debug(" => codec: " + codecname);
if ("gzip".equals(codecname) || "x-gzip".equals(codecname)) {
response.setEntity(new GzipDecompressingEntity(response.getEntity()));
return;
} else if ("deflate".equals(codecname)) {
response.setEntity(new DeflateDecompressingEntity(response.getEntity()));
return;
} else if ("identity".equals(codecname)) {
return;
} else {
throw new ServletException("Unsupported Content-Encoding: " + codecname);
}
}
}
}
}
public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException {
HttpEntity entity = response.getEntity();
if (entity != null) {
Header ceheader = entity.getContentEncoding();
if (ceheader != null) {
HeaderElement[] codecs = ceheader.getElements();
for (HeaderElement h : codecs) {
if (h.getName().equalsIgnoreCase("gzip")) {
response.setEntity(new GzipDecompressingEntity(response.getEntity()));
return;
}
}
}
}
}
private String uncompress(HttpResponse httpResponse) throws ParseException, IOException {
int ret = httpResponse.getStatusLine().getStatusCode();
if (!isOK(ret)) {
return null;
}
// Read the contents
String respBody = null;
HttpEntity entity = httpResponse.getEntity();
String charset = EntityUtils.getContentCharSet(entity);
if (charset == null) {
charset = "UTF-8";
}
// "Content-Encoding"
Header contentEncodingHeader = entity.getContentEncoding();
if (contentEncodingHeader != null) {
String contentEncoding = contentEncodingHeader.getValue();
if (contentEncoding.contains("gzip")) {
respBody = EntityUtils.toString(new GzipDecompressingEntity(entity), charset);
} else if (contentEncoding.contains("deflate")) {
respBody = EntityUtils.toString(new DeflateDecompressingEntity(entity), charset);
}
} else {
// "Content-Type"
Header contentTypeHeader = entity.getContentType();
if (contentTypeHeader != null) {
String contentType = contentTypeHeader.getValue();
if (contentType != null) {
if (contentType.startsWith("application/x-gzip-compressed")) {
respBody = EntityUtils.toString(new GzipDecompressingEntity(entity), charset);
} else if (contentType.startsWith("application/x-deflate")) {
respBody = EntityUtils.toString(new DeflateDecompressingEntity(entity), charset);
}
}
}
}
return respBody;
}
public HttpResponse getHttpResponse(String url) throws ClientProtocolException, IOException
{
HttpResponse response = null;
HttpGet get = new HttpGet(url);
get.addHeader("Accept", "text/html");
get.addHeader("Accept-Charset", "utf-8");
get.addHeader("Accept-Encoding", "gzip");
get.addHeader("Accept-Language", "en-US,en");
int uai = rand.nextInt() % userAgents.size();
if (uai < 0)
{
uai = -uai;
}
get.addHeader("User-Agent", userAgents.get(uai));
response = getHttpClient().execute(get);
HttpEntity entity = response.getEntity();
Header header = entity.getContentEncoding();
if (header != null)
{
HeaderElement[] codecs = header.getElements();
for (int i = 0; i < codecs.length; i++)
{
if (codecs[i].getName().equalsIgnoreCase("gzip"))
{
response.setEntity(new GzipDecompressingEntity(entity));
}
}
}
return response;
}
/**
* Processes the HttpResponse to create a DHisHttpResponse object.
*
* @throws IOException </pre>
*/
private static DhisHttpResponse processResponse( String requestURL, String username, HttpResponse response )
throws Exception
{
DhisHttpResponse dhisHttpResponse;
String output;
int statusCode;
if ( response != null )
{
HttpEntity responseEntity = response.getEntity();
if ( responseEntity != null && responseEntity.getContent() != null )
{
Header contentType = response.getEntity().getContentType();
if ( contentType != null && checkIfGzipContentType( contentType ) )
{
GzipDecompressingEntity gzipDecompressingEntity = new GzipDecompressingEntity( response.getEntity() );
InputStream content = gzipDecompressingEntity.getContent();
output = IOUtils.toString( content, StandardCharsets.UTF_8 );
}
else
{
output = EntityUtils.toString( response.getEntity() );
}
statusCode = response.getStatusLine().getStatusCode();
}
else
{
throw new Exception( "No content found in the response received from http POST call to " + requestURL + " with username " + username );
}
dhisHttpResponse = new DhisHttpResponse( response, output, statusCode );
}
else
{
throw new Exception( "NULL response received from http POST call to " + requestURL + " with username " + username );
}
return dhisHttpResponse;
}
/**
* Get the content of a gzip encoded page
* @param url url from which to read
* @return the String content of the page
* @throws Exception
*/
public static String getGzipedData(String url) throws Exception {
NUHttpGet httpGet = new NUHttpGet(url);
HttpResponse httpResponse = NUHttpClient.getHttpClient().execute(httpGet);
return EntityUtils.toString(new GzipDecompressingEntity(httpResponse.getEntity()));
}
/**
* Get the content of a gzip encoded page
* @param url url from which to read
* @param httpContext the httpContext in which to make the request
* @return the String content of the page
* @throws Exception
*/
public static String getGzipedData(String url, HttpContext httpContext) throws Exception {
NUHttpGet httpGet = new NUHttpGet(url);
HttpResponse httpResponse = NUHttpClient.getHttpClient().execute(httpGet, httpContext);
return EntityUtils.toString(new GzipDecompressingEntity(httpResponse.getEntity()));
}
/**
*
* 从response返回的实体中读取页面代码
*
* @param httpEntity
* Http实体
*
* @return 页面代码
*
* @throws ParseException
*
* @throws IOException
*/
private static String readHtmlContentFromEntity(HttpEntity httpEntity)
throws ParseException, IOException {
String html = "";
Header header = httpEntity.getContentEncoding();
if (httpEntity.getContentLength() < 2147483647L) { // EntityUtils无法处理ContentLength超过2147483647L的Entity
if (header != null && "gzip".equals(header.getValue())) {
html = EntityUtils.toString(new GzipDecompressingEntity(
httpEntity));
} else {
html = EntityUtils.toString(httpEntity);
}
} else {
InputStream in = httpEntity.getContent();
if (header != null && "gzip".equals(header.getValue())) {
html = unZip(in, ContentType.getOrDefault(httpEntity)
.getCharset().toString());
} else {
html = readInStreamToString(in,
ContentType.getOrDefault(httpEntity).getCharset()
.toString());
}
if (in != null) {
in.close();
}
}
return html;
}