`
cppmule
  • 浏览: 449232 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类

HttpClient压缩传输的代码,大部分都不靠谱,apache httpclient 官网的最靠谱,亲测!

    博客分类:
  • java
 
阅读更多

 

HttpClient压缩传输的代码,大部分都不靠谱,apache httpclient 官网的最靠谱,亲测!

------------------------------------------------------------------------------------------------------

/*

 * ====================================================================

 * Licensed to the Apache Software Foundation (ASF) under one

 * or more contributor license agreements.  See the NOTICE file

 * distributed with this work for additional information

 * regarding copyright ownership.  The ASF licenses this file

 * to you under the Apache License, Version 2.0 (the

 * "License"); you may not use this file except in compliance

 * with the License.  You may obtain a copy of the License at

 *

 *   http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing,

 * software distributed under the License is distributed on an

 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY

 * KIND, either express or implied.  See the License for the

 * specific language governing permissions and limitations

 * under the License.

 * ====================================================================

 *

 * This software consists of voluntary contributions made by many

 * individuals on behalf of the Apache Software Foundation.  For more

 * information on the Apache Software Foundation, please see

 * <http://www.apache.org/>.

 *

 */

 

package org.apache.http.examples.client;

 

import java.io.IOException;

 

import org.apache.http.Header;

import org.apache.http.HeaderElement;

import org.apache.http.HttpEntity;

import org.apache.http.HttpException;

import org.apache.http.HttpRequest;

import org.apache.http.HttpRequestInterceptor;

import org.apache.http.HttpResponse;

import org.apache.http.HttpResponseInterceptor;

import org.apache.http.client.entity.GzipDecompressingEntity;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.http.protocol.HttpContext;

import org.apache.http.util.EntityUtils;

 

/**

 * Demonstration of the use of protocol interceptors to transparently

 * modify properties of HTTP messages sent / received by the HTTP client.

 * <p/>

 * In this particular case HTTP client is made capable of transparent content

 * GZIP compression by adding two protocol interceptors: a request interceptor

 * that adds 'Accept-Encoding: gzip' header to all outgoing requests and

 * a response interceptor that automatically expands compressed response

 * entities by wrapping them with a uncompressing decorator class. The use of

 * protocol interceptors makes content compression completely transparent to

 * the consumer of the {@link org.apache.http.client.HttpClient HttpClient}

 * interface.

 */

public class ClientGZipContentCompression {

 

    public final static void main(String[] args) throws Exception {

        DefaultHttpClient httpclient = new DefaultHttpClient();

        try {

            httpclient.addRequestInterceptor(new HttpRequestInterceptor() {

 

                public void process(

                        final HttpRequest request,

                        final HttpContext context) throws HttpException, IOException {

                    if (!request.containsHeader("Accept-Encoding")) {

                        request.addHeader("Accept-Encoding", "gzip");

                    }

                }

 

            });

 

 

            httpclient.addResponseInterceptor(new HttpResponseInterceptor() {

 

                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 (int i = 0; i < codecs.length; i++) {

                                if (codecs[i].getName().equalsIgnoreCase("gzip")) {

                                    response.setEntity(

                                            new GzipDecompressingEntity(response.getEntity()));

                                    return;

                                }

                            }

                        }

                    }

                }

 

            });

 

 

 

            HttpGet httpget = new HttpGet("http://cppmule.zapto.org/acr/acrphone-service/area/cities");

 

            // Execute HTTP request

            System.out.println("executing request " + httpget.getURI());

            HttpResponse response = httpclient.execute(httpget);

 

            System.out.println("----------------------------------------");

            System.out.println(response.getStatusLine());

            System.out.println(response.getLastHeader("Content-Encoding"));

            System.out.println(response.getLastHeader("Content-Length"));

            System.out.println("----------------------------------------");

 

            HttpEntity entity = response.getEntity();

 

            if (entity != null) {

                String content = EntityUtils.toString(entity);

                System.out.println(content);

                System.out.println("----------------------------------------");

                System.out.println("Uncompressed size: "+content.length());

            }

 

        } finally {

            // When HttpClient instance is no longer needed,

            // shut down the connection manager to ensure

            // immediate deallocation of all system resources

            httpclient.getConnectionManager().shutdown();

        }

    }

 

}


 

分享到:
评论

相关推荐

    apache httpclient jar包

    - 使用压缩算法(如gzip)减小传输的数据量。 在实际应用中,开发者可以根据具体需求对HttpClient进行细致的配置和定制,使其更好地适应不同的网络环境和安全要求。HttpClient 4.2.5是一个强大而灵活的工具,对于...

    可用org.apache.commons.httpclient-3.1.0.jar.zip

    import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods....

    org.apache.commons.httpclient相关资源包

    Commons-Logging允许程序在不修改代码的情况下切换到其他日志框架,如Log4j或Java内置的日志系统。 了解这些库后,我们可以深入学习以下关键知识点: 1. **HttpClient的使用**:如何创建HttpClient实例,设置请求...

    apache httpclient源代码,eclipse查看源码

    - 在Eclipse 中,你可以通过"File" -&gt; "Import" -&gt; "Existing Projects into Workspace" 导入Apache HttpClient 4.5 的源代码项目。 - 将下载的压缩包解压,然后在导入对话框中选择解压后的目录,确保"Copy ...

    c++ HttpClient 最新代码

    c++ HttpClient 最新代码c++ HttpClient 最新代码c++ HttpClient 最新代码c++ HttpClient 最新代码c++ HttpClient 最新代码c++ HttpClient 最新代码c++ HttpClient 最新代码c++ HttpClient 最新代码c++ HttpClient ...

    Apache httpclient源码4.5.12

    Apache HttpClient 是一个强大的Java库,用于执行HTTP请求。在4.5.12版本中,它提供了丰富的功能,包括支持HTTP/1.1和部分HTTP/2协议,连接管理,重试策略,以及多种认证机制。这个源码版本是学习HTTP通信、网络编程...

    org.apache.commons.httpclient

    7. **性能优化**:使用连接池提高性能,控制线程,处理大文件传输。 在实际开发中,HttpClient还可以与其他Apache Commons库,如IO和Lang,一起使用,以增强功能,例如处理输入/输出流,字符串操作等。 总的来说,...

    org.apache.commons.httpclient-3.1.jar

    - `LICENSE.txt`:包含了Apache Commons HttpClient的许可协议,它遵循Apache 2.0许可证,允许免费使用和修改源代码。 - `README.txt`:一般提供了项目的简介和快速入门指南。 - `NOTICE.txt`:通常列出库中可能包含...

    org.apache.commons.httpclient相关架包

    标题中的"org.apache.commons.httpclient相关架包"指的是这个库的一系列组件,主要包含在`httpclient.jar`文件中。这个JAR文件包含了HttpClient库的所有必需类和资源,可以被导入到Java项目中以实现HTTP通信功能。 ...

    apache httpclient 4.5.1 doc

    apache httpclient document apache httpcore document

    apache httpclient 源码和 jar包

    4. **错误处理**:通过源码,我们可以看到HttpClient如何处理网络异常、超时等问题,这对于我们在实际开发中遇到类似问题时提供解决方案有很大的帮助。 jar包部分: 1. **使用方式**:HttpClient的jar包包含了所有...

    org.apache.commons.httpclient 远程下载文件

    此外,Apache HttpClient库已经被弃用,现在推荐使用Java 7及更高版本内置的`java.net.HttpURLConnection`或更现代的库如Apache HttpClient 4.x或OkHttp。不过,对于理解HTTP客户端编程的基本原理,HttpClient仍然是...

    apache-httpclient应用所有jar

    它是Apache软件基金会的Jakarta项目的一部分,被广泛用于开发需要与Web服务器交互的应用程序。在本文中,我们将深入探讨HttpClient的核心概念、主要功能以及如何使用它来实现网络爬虫。 **HttpClient 主要特点** 1...

    org.apache.commons.httpclient资源包(4.2)

    Apache Commons HttpClient是一个流行...无论你是构建Web服务客户端、爬虫,还是进行任何需要与Web服务器交互的应用,这个资源包都是不可或缺的。使用HttpClient,可以高效、灵活地实现HTTP通信,提升应用的网络功能。

    wechatpay-apache-httpclient-0.2.1.jar

    wechatpay-apache-httpclient-0.2.1.jar

    httpClient 网络传输的实例

    本实例将深入讲解如何使用HttpClient进行网络传输。 一、HttpClient基本概念 HttpClient是一个实现了HTTP/1.0和HTTP/1.1协议的客户端编程工具包。它不仅支持基本的HTTP方法,还支持HTTPS、Cookie管理、连接池、...

    apache_httpclient 高清 pdf

    2. HttpClient不能做的事情 明确地讲,HttpClient并不是一个网络浏览器,它不会缓存内容,不会执行HTML中的JavaScript代码,也不会猜测内容类型或者对请求/重定向URI进行重新格式化。其功能仅限于发送和接收HTTP报文...

    httpclient 4.0.3 源代码

    《HttpClient 4.0.3源代码解析》 HttpClient是一个由Apache基金会开发的开源HTTP客户端API,广泛应用于Java编程环境中,用于实现与HTTP服务器的通信。版本4.0.3是HttpClient的一个稳定版本,提供了丰富的功能和改进...

Global site tag (gtag.js) - Google Analytics