httpClient對post內容gzip壓縮和server端解壓接收
阿新 • • 發佈:2019-01-07
client端程式碼:
- publicvoid sendHttp(String url, String message) {
- if (StringUtils.isBlank(message)) {
- LOGGER.info("a blank message, return.");
- return;
- }
- PostMethod postMethod = new PostMethod(url);
- postMethod.setContentChunked(true);
-
postMethod.addRequestHeader("Accept"
- postMethod.setRequestHeader("Content-Encoding", "gzip");
- postMethod.setRequestHeader("Transfer-Encoding", "chunked");
- try {
- ByteArrayOutputStream originalContent = new ByteArrayOutputStream();
- originalContent
-
.write(message.getBytes(Charset.forName("UTF-8"
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- GZIPOutputStream gzipOut = new GZIPOutputStream(baos);
- originalContent.writeTo(gzipOut);
- gzipOut.finish();
- postMethod.setRequestEntity(new ByteArrayRequestEntity(baos
-
.toByteArray(), "text/plain; charset=utf-8"
- } catch (IOException e) {
- LOGGER.error("write message fail.", e);
- return;
- }
- int retry = 0;
- do {
- try {
- int status = httpClient.executeMethod(postMethod);
- if (HttpStatus.SC_OK == status) {
- if (LOGGER.isDebugEnabled()) {
- LOGGER.debug("send http success, url=" + url
- + ", content=" + message);
- }
- return;
- } else {
- String rsp = postMethod.getResponseBodyAsString();
- LOGGER.error("send http fail, status is: " + status
- + ", response is: " + rsp);
- }
- } catch (HttpException e) {
- LOGGER.info("http exception when send http.", e);
- } catch (IOException e) {
- LOGGER.info("io exception when send http.", e);
- } finally {
- postMethod.releaseConnection();
- }
- LOGGER.info("this is "+ retry + " time, try next");
- } while (retry++ < 3);
server端使用servlet Filter對request請求進行處理,無論後端是哪類web框架都能適配。
- /**
- *
- */
- package filter;
- import java.io.IOException;
- import javax.servlet.Filter;
- import javax.servlet.FilterChain;
- import javax.servlet.FilterConfig;
- import javax.servlet.ServletException;
- import javax.servlet.ServletRequest;
- import javax.servlet.ServletResponse;
- import javax.servlet.http.HttpServletRequest;
- /**
- * 如果請求訊息中包含gzip壓縮資料,則進行解壓
- *
- * @author chunxi.lcx
- *
- */
- publicclass GzipFilter implements Filter {
- @Override
- publicvoid init(FilterConfig filterConfig) throws ServletException {
- }
- @Override
- publicvoid doFilter(ServletRequest request, ServletResponse response,
- FilterChain chain) throws IOException, ServletException {
- chain.doFilter(new GzipRequestWrapper((HttpServletRequest) request),
- response);
- }
- @Override
- publicvoid destroy() {
- }
- }
- /**
- *
- */
- package filter;
- import java.io.IOException;
- import java.util.zip.GZIPInputStream;
- import javax.servlet.ServletInputStream;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletRequestWrapper;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- /**
- * @author chunxi.lcx
- *
- */
- publicclass GzipRequestWrapper extends HttpServletRequestWrapper {
- publicstaticfinal Logger LOGGER = LoggerFactory
- .getLogger(GzipRequestWrapper.class);
- private HttpServletRequest request;
- public GzipRequestWrapper(HttpServletRequest request) {
- super(request);
- this.request = request;
- }
- @Override
- public ServletInputStream getInputStream() throws IOException {
- ServletInputStream stream = request.getInputStream();
- String contentEncoding = request.getHeader("Content-Encoding");
- // 如果對內容進行了壓縮,則解壓
- if (null != contentEncoding && contentEncoding.indexOf("gzip") != -1) {
- try {
- final GZIPInputStream gzipInputStream = new GZIPInputStream(
- stream);
- ServletInputStream newStream = new ServletInputStream() {
- @Override
- publicint read() throws IOException {
- return gzipInputStream.read();
- }
- };
- return newStream;
- } catch (Exception e) {
- LOGGER.debug("ungzip content fail.", e);
- }
- }
- return stream;
- }
- }
在web.xml的合適位置配置過濾器:
- <filter>
- <filter-name>GzipFilter</filter-name>
- <filter-class>com.taobao.xray.filter.GzipFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>GzipFilter</filter-name>
- <url-pattern>/metrics/putLines</url-pattern>
- </filter-mapping>