httpClient对post内容gzip压缩和server端解压接收

来源:互联网 发布:memached 端口信息 编辑:程序博客网 时间:2024/06/05 17:07

client端代码:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public void sendHttp(String url, String message) {  
  2.     if (StringUtils.isBlank(message)) {  
  3.         LOGGER.info("a blank message, return.");  
  4.         return;  
  5.     }  
  6.     PostMethod postMethod = new PostMethod(url);  
  7.     postMethod.setContentChunked(true);  
  8.     postMethod.addRequestHeader("Accept""text/plain");  
  9.     postMethod.setRequestHeader("Content-Encoding""gzip");  
  10.     postMethod.setRequestHeader("Transfer-Encoding""chunked");  
  11.   
  12.     try {  
  13.         ByteArrayOutputStream originalContent = new ByteArrayOutputStream();  
  14.         originalContent  
  15.                 .write(message.getBytes(Charset.forName("UTF-8")));  
  16.   
  17.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  18.         GZIPOutputStream gzipOut = new GZIPOutputStream(baos);  
  19.         originalContent.writeTo(gzipOut);  
  20.         gzipOut.finish();  
  21.   
  22.         postMethod.setRequestEntity(new ByteArrayRequestEntity(baos  
  23.                 .toByteArray(), "text/plain; charset=utf-8"));  
  24.     } catch (IOException e) {  
  25.         LOGGER.error("write message fail.", e);  
  26.         return;  
  27.     }  
  28.   
  29.     int retry = 0;  
  30.     do {  
  31.         try {  
  32.             int status = httpClient.executeMethod(postMethod);  
  33.             if (HttpStatus.SC_OK == status) {  
  34.                 if (LOGGER.isDebugEnabled()) {  
  35.                     LOGGER.debug("send http success, url=" + url  
  36.                             + ", content=" + message);  
  37.                 }  
  38.                 return;  
  39.             } else {  
  40.                 String rsp = postMethod.getResponseBodyAsString();  
  41.                 LOGGER.error("send http fail, status is: " + status  
  42.                         + ", response is: " + rsp);  
  43.             }  
  44.         } catch (HttpException e) {  
  45.             LOGGER.info("http exception when send http.", e);  
  46.         } catch (IOException e) {  
  47.             LOGGER.info("io exception when send http.", e);  
  48.         } finally {  
  49.             postMethod.releaseConnection();  
  50.         }  
  51.         LOGGER.info("this is "+ retry + " time, try next");  
  52.     } while (retry++ < 3);  


server端使用servlet Filter对request请求进行处理,无论后端是哪类web框架都能适配。

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /** 
  2.  *  
  3.  */  
  4. package filter;  
  5.   
  6. import java.io.IOException;  
  7. import javax.servlet.Filter;  
  8. import javax.servlet.FilterChain;  
  9. import javax.servlet.FilterConfig;  
  10. import javax.servlet.ServletException;  
  11. import javax.servlet.ServletRequest;  
  12. import javax.servlet.ServletResponse;  
  13. import javax.servlet.http.HttpServletRequest;  
  14.   
  15. /** 
  16.  * 如果请求消息中包含gzip压缩数据,则进行解压 
  17.  *  
  18.  * @author chunxi.lcx 
  19.  *  
  20.  */  
  21. public class GzipFilter implements Filter {  
  22.   
  23.     @Override  
  24.     public void init(FilterConfig filterConfig) throws ServletException {  
  25.   
  26.     }  
  27.   
  28.     @Override  
  29.     public void doFilter(ServletRequest request, ServletResponse response,  
  30.             FilterChain chain) throws IOException, ServletException {  
  31.         chain.doFilter(new GzipRequestWrapper((HttpServletRequest) request),  
  32.                 response);  
  33.     }  
  34.   
  35.     @Override  
  36.     public void destroy() {  
  37.   
  38.     }  
  39.   
  40. }  

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /** 
  2.  *  
  3.  */  
  4. package filter;  
  5.   
  6. import java.io.IOException;  
  7. import java.util.zip.GZIPInputStream;  
  8.   
  9. import javax.servlet.ServletInputStream;  
  10. import javax.servlet.http.HttpServletRequest;  
  11. import javax.servlet.http.HttpServletRequestWrapper;  
  12.   
  13. import org.slf4j.Logger;  
  14. import org.slf4j.LoggerFactory;  
  15.   
  16. /** 
  17.  * @author chunxi.lcx 
  18.  *  
  19.  */  
  20. public class GzipRequestWrapper extends HttpServletRequestWrapper {  
  21.     public static final Logger LOGGER = LoggerFactory  
  22.             .getLogger(GzipRequestWrapper.class);  
  23.     private HttpServletRequest request;  
  24.   
  25.     public GzipRequestWrapper(HttpServletRequest request) {  
  26.         super(request);  
  27.         this.request = request;  
  28.     }  
  29.   
  30.     @Override  
  31.     public ServletInputStream getInputStream() throws IOException {  
  32.         ServletInputStream stream = request.getInputStream();  
  33.         String contentEncoding = request.getHeader("Content-Encoding");  
  34.         // 如果对内容进行了压缩,则解压  
  35.         if (null != contentEncoding && contentEncoding.indexOf("gzip") != -1) {  
  36.             try {  
  37.                 final GZIPInputStream gzipInputStream = new GZIPInputStream(  
  38.                         stream);  
  39.   
  40.                 ServletInputStream newStream = new ServletInputStream() {  
  41.   
  42.                     @Override  
  43.                     public int read() throws IOException {  
  44.                         return gzipInputStream.read();  
  45.                     }  
  46.                 };  
  47.                 return newStream;  
  48.             } catch (Exception e) {  
  49.                 LOGGER.debug("ungzip content fail.", e);  
  50.             }  
  51.         }  
  52.         return stream;  
  53.     }  
  54.   
  55. }  

在web.xml的合适位置配置过滤器:

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1.    <filter>  
  2.     <filter-name>GzipFilter</filter-name>  
  3.     <filter-class>com.taobao.xray.filter.GzipFilter</filter-class>  
  4. </filter>  
  5. <filter-mapping>  
  6.     <filter-name>GzipFilter</filter-name>  
  7.     <url-pattern>/metrics/putLines</url-pattern>  
  8. </filter-mapping>  

0 0