c http post 请求的post请求中如何使用gzip 使压缩率最高

22803人阅读
client端代码:
public void sendHttp(String url, String message) {
if (StringUtils.isBlank(message)) {
(&a blank message, return.&);
PostMethod postMethod = new PostMethod(url);
postMethod.setContentChunked(true);
postMethod.addRequestHeader(&Accept&, &text/plain&);
postMethod.setRequestHeader(&Content-Encoding&, &gzip&);
postMethod.setRequestHeader(&Transfer-Encoding&, &chunked&);
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/ charset=utf-8&));
} catch (IOException e) {
LOGGER.error(&write message fail.&, e);
int retry = 0;
int status = httpClient.executeMethod(postMethod);
if (HttpStatus.SC_OK == status) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(&send http success, url=& + url
+ &, content=& + message);
String rsp = postMethod.getResponseBodyAsString();
LOGGER.error(&send http fail, status is: & + status
+ &, response is: & + rsp);
} catch (HttpException e) {
(&http exception when send http.&, e);
} catch (IOException e) {
(&io exception when send http.&, e);
} finally {
postMethod.releaseConnection();
(&this is &+ retry + & time, try next&);
} while (retry++ & 3);
server端使用servlet Filter对request请求进行处理,无论后端是哪类web框架都能适配。
import java.io.IOE
import javax.servlet.F
import javax.servlet.FilterC
import javax.servlet.FilterC
import javax.servlet.ServletE
import javax.servlet.ServletR
import javax.servlet.ServletR
import javax.servlet.http.HttpServletR
* 如果请求消息中包含gzip压缩数据,则进行解压
* @author chunxi.lcx
public class GzipFilter implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
chain.doFilter(new GzipRequestWrapper((HttpServletRequest) request),
response);
public void destroy() {
import java.io.IOE
import java.util.zip.GZIPInputS
import javax.servlet.ServletInputS
import javax.servlet.http.HttpServletR
import javax.servlet.http.HttpServletRequestW
import org.slf4j.L
import org.slf4j.LoggerF
* @author chunxi.lcx
public class GzipRequestWrapper extends HttpServletRequestWrapper {
public static final Logger LOGGER = LoggerFactory
.getLogger(GzipRequestWrapper.class);
private HttpServletR
public GzipRequestWrapper(HttpServletRequest request) {
super(request);
this.request =
public ServletInputStream getInputStream() throws IOException {
ServletInputStream stream = request.getInputStream();
String contentEncoding = request.getHeader(&Content-Encoding&);
// 如果对内容进行了压缩,则解压
if (null != contentEncoding && contentEncoding.indexOf(&gzip&) != -1) {
final GZIPInputStream gzipInputStream = new GZIPInputStream(
ServletInputStream newStream = new ServletInputStream() {
public int read() throws IOException {
return gzipInputStream.read();
return newS
} catch (Exception e) {
LOGGER.debug(&ungzip content fail.&, e);
在web.xml的合适位置配置过滤器:
&filter-name&GzipFilter&/filter-name&
&filter-class&com.taobao.xray.filter.GzipFilter&/filter-class&
&filter-mapping&
&filter-name&GzipFilter&/filter-name&
&url-pattern&/metrics/putLines&/url-pattern&
&/filter-mapping&
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:121612次
积分:1087
积分:1087
排名:千里之外
原创:20篇
评论:10条
(2)(1)(3)(1)(1)(3)(2)(1)(2)(5)http&client对post内容gzip压缩和server端解压接收
client端代码:
server端使用servlet Filter对request请求进行处理,无论后端是哪类web框架都能适配。
在web.xml的合适位置配置过滤器:
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。httpClient对post内容gzip压缩和server端解压接收 - 博客频道 - CSDN.NET
末路狂奔》》》》
宁静以致远,温文尔雅。
分类:javaweb
client端代码:
server端使用servlet Filter对request请求进行处理,无论后端是哪类web框架都能适配。
在web.xml的合适位置配置过滤器:
排名:第15793名
(22)(11)(1)(11)(18)(11)(15)(1)(0)(14)(24)(2)(25)(9)(14)(11)(7)(1)(11)(2)(0)(1)(0)(6)(1)(5)(0)(6)(0)(3)(0)(1)(2)(3)(2)(3)(0)(1)(2)(12)(0)(2)(2)(1)(16)(0)(0)(0)(2)(0)(2)(10)(3)(3)(5)(1)(2)(3)(2)(1)(1)(5)(5)(5)22805人阅读
client端代码:
public void sendHttp(String url, String message) {
if (StringUtils.isBlank(message)) {
(&a blank message, return.&);
PostMethod postMethod = new PostMethod(url);
postMethod.setContentChunked(true);
postMethod.addRequestHeader(&Accept&, &text/plain&);
postMethod.setRequestHeader(&Content-Encoding&, &gzip&);
postMethod.setRequestHeader(&Transfer-Encoding&, &chunked&);
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/ charset=utf-8&));
} catch (IOException e) {
LOGGER.error(&write message fail.&, e);
int retry = 0;
int status = httpClient.executeMethod(postMethod);
if (HttpStatus.SC_OK == status) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(&send http success, url=& + url
+ &, content=& + message);
String rsp = postMethod.getResponseBodyAsString();
LOGGER.error(&send http fail, status is: & + status
+ &, response is: & + rsp);
} catch (HttpException e) {
(&http exception when send http.&, e);
} catch (IOException e) {
(&io exception when send http.&, e);
} finally {
postMethod.releaseConnection();
(&this is &+ retry + & time, try next&);
} while (retry++ & 3);
server端使用servlet Filter对request请求进行处理,无论后端是哪类web框架都能适配。
import java.io.IOE
import javax.servlet.F
import javax.servlet.FilterC
import javax.servlet.FilterC
import javax.servlet.ServletE
import javax.servlet.ServletR
import javax.servlet.ServletR
import javax.servlet.http.HttpServletR
* 如果请求消息中包含gzip压缩数据,则进行解压
* @author chunxi.lcx
public class GzipFilter implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
chain.doFilter(new GzipRequestWrapper((HttpServletRequest) request),
response);
public void destroy() {
import java.io.IOE
import java.util.zip.GZIPInputS
import javax.servlet.ServletInputS
import javax.servlet.http.HttpServletR
import javax.servlet.http.HttpServletRequestW
import org.slf4j.L
import org.slf4j.LoggerF
* @author chunxi.lcx
public class GzipRequestWrapper extends HttpServletRequestWrapper {
public static final Logger LOGGER = LoggerFactory
.getLogger(GzipRequestWrapper.class);
private HttpServletR
public GzipRequestWrapper(HttpServletRequest request) {
super(request);
this.request =
public ServletInputStream getInputStream() throws IOException {
ServletInputStream stream = request.getInputStream();
String contentEncoding = request.getHeader(&Content-Encoding&);
// 如果对内容进行了压缩,则解压
if (null != contentEncoding && contentEncoding.indexOf(&gzip&) != -1) {
final GZIPInputStream gzipInputStream = new GZIPInputStream(
ServletInputStream newStream = new ServletInputStream() {
public int read() throws IOException {
return gzipInputStream.read();
return newS
} catch (Exception e) {
LOGGER.debug(&ungzip content fail.&, e);
在web.xml的合适位置配置过滤器:
&filter-name&GzipFilter&/filter-name&
&filter-class&com.taobao.xray.filter.GzipFilter&/filter-class&
&filter-mapping&
&filter-name&GzipFilter&/filter-name&
&url-pattern&/metrics/putLines&/url-pattern&
&/filter-mapping&
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:121614次
积分:1087
积分:1087
排名:千里之外
原创:20篇
评论:10条
(2)(1)(3)(1)(1)(3)(2)(1)(2)(5)}

我要回帖

更多关于 httpclient post gzip 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信