本文整理匯總了Java中org.apache.http.client.entity.GzipDecompressingEntity類的典型用法代碼示例。如果您正苦於以下問題:Java GzipDecompressingEntity類的具體用法?Java GzipDecompressingEntity怎麽用?Java GzipDecompressingEntity使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
GzipDecompressingEntity類屬於org.apache.http.client.entity包,在下文中一共展示了GzipDecompressingEntity類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: buildHttpClient
import org.apache.http.client.entity.GzipDecompressingEntity; //導入依賴的package包/類
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;
}
示例2: testContentEncodingRequestParameter
import org.apache.http.client.entity.GzipDecompressingEntity; //導入依賴的package包/類
@Test
public void testContentEncodingRequestParameter() throws Exception {
final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
final StringEntity original = new StringEntity("encoded stuff");
original.setContentEncoding("GZip");
response.setEntity(original);
final RequestConfig config = RequestConfig.custom()
.setDecompressionEnabled(false)
.build();
final HttpContext context = new BasicHttpContext();
context.setAttribute(HttpClientContext.REQUEST_CONFIG, config);
final HttpResponseInterceptor interceptor = new ResponseContentEncoding();
interceptor.process(response, context);
final HttpEntity entity = response.getEntity();
Assert.assertNotNull(entity);
Assert.assertFalse(entity instanceof GzipDecompressingEntity);
}
示例3: initClient
import org.apache.http.client.entity.GzipDecompressingEntity; //導入依賴的package包/類
private HttpClient initClient() {
DefaultHttpClient client = new DefaultHttpClient();
client.addRequestInterceptor((request, context) -> {
if (!request.containsHeader(ACCEPT_ENCODING)) {
request.addHeader(ACCEPT_ENCODING, GZIP);
}
});
client.addResponseInterceptor((response, context) -> {
HttpEntity entity = response.getEntity();
if (entity != null) {
Header header = entity.getContentEncoding();
if (header != null) {
HeaderElement[] elements = header.getElements();
for (int i = 0; i < elements.length; i++) {
if (elements[i].getName().equalsIgnoreCase(GZIP)) {
response.setEntity(
new GzipDecompressingEntity(response.getEntity()));
return;
}
}
}
}
});
return client;
}
示例4: process
import org.apache.http.client.entity.GzipDecompressingEntity; //導入依賴的package包/類
@Override
public void process(HttpResponse response, HttpContext context)
throws HttpException, IOException {
HttpEntity entity = response.getEntity();
if (entity != null) {
Header ceheader = entity.getContentEncoding();
if (ceheader != null) {
HeaderElement[] codecs = ceheader.getElements();
for (int i = 0; i < codecs.length; i++) {
if (codecs[i].getName().equalsIgnoreCase("gzip")) {
response.setEntity(new GzipDecompressingEntity(
response.getEntity()));
return;
}
}
}
}
}
示例5: getResponseString
import org.apache.http.client.entity.GzipDecompressingEntity; //導入依賴的package包/類
public static String getResponseString(HttpResponse response) {
if (response == null) {
return null;
}
try {
HttpEntity entity = response.getEntity();
Header contentEncoding = entity.getContentEncoding();
if (contentEncoding != null) {
if (contentEncoding.getValue().toLowerCase(Locale.getDefault())
.contains("gzip")) {
GzipDecompressingEntity gzipEntity = new GzipDecompressingEntity(
entity);
return EntityUtils.toString(gzipEntity);
}
}
return EntityUtils.toString(entity);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
示例6: process
import org.apache.http.client.entity.GzipDecompressingEntity; //導入依賴的package包/類
@Override
public void process(HttpResponse response, HttpContext context) throws HttpException, IOException {
HttpEntity entity = response.getEntity();
if (entity != null) {
Header ceheader = entity.getContentEncoding();
if (ceheader != null) {
HeaderElement[] codecs = ceheader.getElements();
for (int i = 0; i < codecs.length; i++) {
if (codecs[i].getName().equalsIgnoreCase("gzip")) {
response.setEntity(
new GzipDecompressingEntity(response.getEntity()));
return;
}
}
}
}
}
示例7: process
import org.apache.http.client.entity.GzipDecompressingEntity; //導入依賴的package包/類
@Override
public void process(HttpResponse httpResponse, HttpContext httpContext)
throws HttpException, IOException
{
HttpEntity entity = httpResponse.getEntity();
if (entity != null)
{
Header ceheader = entity.getContentEncoding();
if (ceheader != null)
{
HeaderElement[] codecs = ceheader.getElements();
for (int i = 0; i < codecs.length; i++)
{
if (codecs[i].getName().equalsIgnoreCase("gzip"))
{
httpResponse.setEntity(new GzipDecompressingEntity(
httpResponse.getEntity()));
return;
}
}
}
}
}
示例8: postJSON
import org.apache.http.client.entity.GzipDecompressingEntity; //導入依賴的package包/類
public JSONObject postJSON() throws JSONException, NForumException, IOException{
HttpResponse response = httpClient.execute(httpPost);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != 200)
throw new NForumException(NForumException.EXCEPTION_NETWORK + ":" + statusCode);
Header header = response.getEntity().getContentEncoding();
if (header != null) {
for (HeaderElement element : header.getElements()) {
if (element.getName().equalsIgnoreCase("gzip")) {
response.setEntity(new GzipDecompressingEntity(response.getEntity()));
break;
}
}
}
String result = ResponseProcessor.getStringFromResponse(response);
httpPost.abort();
JSONObject json = new JSONObject(result);
if(json.has("msg")){
throw new NForumException(json.getString("msg"));
}
return json;
}
示例9: getJSON
import org.apache.http.client.entity.GzipDecompressingEntity; //導入依賴的package包/類
public JSONObject getJSON() throws JSONException, NForumException, IOException{
HttpResponse response = httpClient.execute(httpGet);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != 200)
throw new NForumException(NForumException.EXCEPTION_NETWORK + ":" + statusCode);
Header header = response.getEntity().getContentEncoding();
if (header != null) {
for (HeaderElement element : header.getElements()) {
if (element.getName().equalsIgnoreCase("gzip")) {
response.setEntity(new GzipDecompressingEntity(response.getEntity()));
break;
}
}
}
String result = ResponseProcessor.getStringFromResponse(response);
httpGet.abort();
JSONObject json = new JSONObject(result);
if(json.has("msg")){
throw new NForumException(json.getString("msg"));
}
return json;
}
示例10: process
import org.apache.http.client.entity.GzipDecompressingEntity; //導入依賴的package包/類
@Override
public void process(HttpResponse response, HttpContext context) throws HttpException, IOException {
HttpEntity entity = response.getEntity();
if (entity != null) {
Header contentEncoding = entity.getContentEncoding();
if (contentEncoding != null) {
HeaderElement[] codecs = contentEncoding.getElements();
for (HeaderElement codec : codecs) {
if (codec.getName().equalsIgnoreCase(GZIP)) {
response.setEntity(new GzipDecompressingEntity(response.getEntity()));
return;
}
if (codec.getName().equalsIgnoreCase(DEFLATE)) {
response.setEntity(new DeflateDecompressingEntity(response.getEntity()));
return;
}
}
}
}
}
示例11: handleContentEncoding
import org.apache.http.client.entity.GzipDecompressingEntity; //導入依賴的package包/類
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);
}
}
}
}
}
示例12: getHttpResponse
import org.apache.http.client.entity.GzipDecompressingEntity; //導入依賴的package包/類
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;
}
示例13: process
import org.apache.http.client.entity.GzipDecompressingEntity; //導入依賴的package包/類
/**
* Handles the following {@code Content-Encoding}s by
* using the appropriate decompressor to wrap the response Entity:
* <ul>
* <li>gzip - see {@link GzipDecompressingEntity}</li>
* <li>deflate - see {@link DeflateDecompressingEntity}</li>
* <li>identity - no action needed</li>
* </ul>
*
* @param response the response which contains the entity
* @param context not currently used
*
* @throws HttpException if the {@code Content-Encoding} is none of the above
*/
public void process(
final HttpResponse response,
final HttpContext context) throws HttpException, IOException {
HttpEntity entity = response.getEntity();
// It wasn't a 304 Not Modified response, 204 No Content or similar
if (entity != null) {
Header ceheader = entity.getContentEncoding();
if (ceheader != null) {
HeaderElement[] codecs = ceheader.getElements();
for (HeaderElement codec : codecs) {
String codecname = codec.getName().toLowerCase(Locale.US);
if ("gzip".equals(codecname) || "x-gzip".equals(codecname)) {
response.setEntity(new GzipDecompressingEntity(response.getEntity()));
if (context != null) context.setAttribute(UNCOMPRESSED, true);
return;
} else if ("deflate".equals(codecname)) {
response.setEntity(new DeflateDecompressingEntity(response.getEntity()));
if (context != null) context.setAttribute(UNCOMPRESSED, true);
return;
} else if ("identity".equals(codecname)) {
/* Don't need to transform the content - no-op */
return;
} else {
throw new HttpException("Unsupported Content-Coding: " + codec.getName());
}
}
}
}
}
示例14: handleCompressedEntity
import org.apache.http.client.entity.GzipDecompressingEntity; //導入依賴的package包/類
public static HttpEntity handleCompressedEntity(HttpEntity entity) {
org.apache.http.Header contentEncoding = entity.getContentEncoding();
if (contentEncoding != null)
for (HeaderElement e : contentEncoding.getElements()) {
if (CONTENT_ENCODING_GZIP.equalsIgnoreCase(e.getName())) {
return new GzipDecompressingEntity(entity);
}
if (CONTENT_ENCODING_DEFLATE.equalsIgnoreCase(e.getName())) {
return new DeflateDecompressingEntity(entity);
}
}
return entity;
}
示例15: createGzipResponseInterceptor
import org.apache.http.client.entity.GzipDecompressingEntity; //導入依賴的package包/類
/**
* If gzip is enabled, this will decode things.
* @return
*/
private HttpResponseInterceptor createGzipResponseInterceptor()
{
return new HttpResponseInterceptor()
{
@Override
public void process( HttpResponse response, HttpContext context )
throws HttpException, IOException
{
//..get the entity
final HttpEntity entity = response.getEntity();
if ( entity == null )
return;
//..Get any content encoding headers
final Header ceHeader = entity.getContentEncoding();
if ( ceHeader == null )
return;
//..Get any entries
HeaderElement[] codecs = ceHeader.getElements();
//..See if one is marked as gzip
for ( final HeaderElement codec : codecs )
{
if ( codec.getName().equalsIgnoreCase( "gzip" ))
{
//..Hey, it's gzip! decompress the entity
response.setEntity( new GzipDecompressingEntity( response.getEntity()));
//..Done with this ish.
return;
}
}
}
};
}