发送请求(要求服务端对response进行GZip压缩):
- import org.apache.commons.httpclient.HttpClient;
- import org.apache.commons.httpclient.HttpStatus;
- public class TestGzip {
- private final static String url = "http://localhost:8888/ltest.jsp";
- public static void main(String[] args) throws Exception{
- HttpClient http = new HttpClient();
- CustomGetMethod get = new CustomGetMethod(url);
- //添加头信息告诉服务端可以对Response进行GZip压缩
- get.setRequestHeader("Accept-Encoding", "gzip, deflate");
- try {
- int statusCode = http.executeMethod(get);
- if (statusCode != HttpStatus.SC_OK) {
- System.err.println("Method failed: "
- + get.getStatusLine());
- }
- //打印解压后的返回信息
- System.out.println(get.getResponseBodyAsString());
- } catch (Exception e) {
- System.err.println("页面无法访问");
- e.printStackTrace();
- } finally {
- get.releaseConnection();
- }
- }
- }
下面是CustomGetMethod.java的内容,getResponseBodyAsString()方法被重写,加入了解压功能
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.util.zip.GZIPInputStream;
- public class CustomGetMethod extends org.apache.commons.httpclient.methods.GetMethod{
- public CustomGetMethod(String uri) {
- super(uri);
- }
- /**
- * Get response as string whether response is GZipped or not
- *
- * @return
- * @throws IOException
- */
- @Override
- public String getResponseBodyAsString() throws IOException {
- GZIPInputStream gzin;
- if (getResponseBody() != null || getResponseStream() != null) {
- if(getResponseHeader("Content-Encoding") != null
- && getResponseHeader("Content-Encoding").getValue().toLowerCase().indexOf("gzip") > -1) {
- //For GZip response
- InputStream is = getResponseBodyAsStream();
- gzin = new GZIPInputStream(is);
- InputStreamReader isr = new InputStreamReader(gzin, getResponseCharSet());
- java.io.BufferedReader br = new java.io.BufferedReader(isr);
- StringBuffer sb = new StringBuffer();
- String tempbf;
- while ((tempbf = br.readLine()) != null) {
- sb.append(tempbf);
- sb.append("\r\n");
- }
- isr.close();
- gzin.close();
- return sb.toString();
- } else {
- //For deflate response
- return super.getResponseBodyAsString();
- }
- } else {
- return null;
- }
- }
- }
相关推荐
在这个例子中,我们设置了HttpClientHandler的`AutomaticDecompression`属性为`DecompressionMethods.GZip`,这样HttpClient就会自动处理GZip格式的压缩响应。 二、HttpClientHandler和DecompressionMethods枚举 ...
去取解压 解压缩用 gzip 压缩的响应体。安装 npm install --save go-fetch-decompress用法 var HttpClient = require('go-fetch');var decompress = require('go-fetch-decompress');var parseBody = require('go-...
标题 "java抓取网页源码gzip-phproxy" 暗示了我们正在讨论一个使用Java进行Web抓取,并处理GZIP压缩的项目,而“PHP中的代理”则可能指的是使用PHP构建的代理服务器。这个项目可能涉及到如何通过Java从通过PHP代理...
这种格式结合了tar(归档)和gzip(压缩)两种工具,能够有效地减小文件体积,便于在网络上传输和存储。在下载完成后,我们需要先解压这个文件,然后通过Python的setuptools或pip工具进行安装。 解压"EasyConnect-...
3. **使用GZIP压缩进行文本数据传输**: - 使用GZIP压缩可以显著减小传输的数据量,从而降低网络传输消耗的电量。可以通过Java自带的`GZIPInputStream`类来实现数据的解压: ```java ...
5. **处理压缩内容**:如果内容被压缩,比如使用gzip,我们可以使用`System.IO.Compression.GZipStream`来解压。解压后的内容再按照指定的字符集进行解码。 6. **处理编码**:对于非ASCII字符,我们需要使用合适的...
29. `System.IO.Compression.GZipStream` 和 `System.IO.Compression.DeflateStream`:GZIP和Deflate压缩流,用于数据压缩和解压。 30. `System.Net.Sockets.TcpClient` 和 `System.Net.Sockets.TcpListener`:TCP...
8. **性能优化**:减少网络请求次数,合并多个请求为一个(如批量操作),使用缓存策略,以及在可能的情况下使用GZIP压缩以减小数据传输量。 通过阅读"Consume-Webservice-in-Windows-Universal-App.pdf"和解压...