HttpCLient实现对被GZip压缩过的Response进行解压
来源:互联网 发布:spss数据统计与分析pdf 编辑:程序博客网 时间:2024/06/10 18:41
发送http请求时,设置请求头Accept-Encoding:gzip, deflate,则服务器会返回压缩的内容。
若不设置,相应内容则正常返回。
发送请求(要求服务端对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;
- }
- }
- }
0 1
- HttpCLient实现对被GZip压缩过的Response进行解压
- HttpCLient实现对被GZip压缩过的Response进行解压
- HttpCLient实现对被GZip压缩过的Response进行解压
- HttpCLient实现对被GZip压缩过的Response进行解压
- 乱码被GZip压缩过的Response进行解压
- [Java]使用gzip对字符串进行压缩/解压
- Java实现Gzip的压缩和解压
- 过滤器通过HttpServletResponseWrapper包装HttpServletResponse实现获取response中的返回数据,以及对数据进行gzip压缩
- GZip 压缩解压 --- Pako 的压缩解压
- httpClient对post内容gzip压缩和server端解压接收
- Gzip对数据进行压缩
- Java使用GZIP进行压缩和解压
- C# GZip对字符串压缩和解压
- C# GZip对字符串压缩和解压
- GZip对字符串压缩和解压
- C# GZip对字符串压缩和解压
- 使用zlib实现gzip格式数据的压缩和解压
- 使用zlib实现gzip格式数据的压缩和解压
- Linux编程基础——信号
- 队列的实现(链表方式)
- ReactNative环境搭建
- ConcurrentLinkedQueue使用陷阱(一)
- PL/SQL Developer AutoReplace (设置常用SQL快捷键)
- HttpCLient实现对被GZip压缩过的Response进行解压
- Excel保存到数据库时数据精度的处理
- Android出现“Read-only file system”解决办法
- objdump生成可读的汇编代码
- 南宁市究竟有多少家跨境购物体验中心?
- SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder”问题解决
- #午安,努力#11.29
- 结构体(struct) 的 sizeof 大小
- Edit Distance