写了一个gzip压缩发送请求
public static String compressSendPost(String postURL,String topicName, String message) {
String result = "";
try {
PostMethod postMethod = null;
postMethod = new PostMethod(postURL) ;
//添加请求头数据
postMethod.setContentChunked(true);
postMethod.addRequestHeader("Accept", "text/plain");
postMethod.setRequestHeader("Content-Encoding", "gzip");
postMethod.setRequestHeader("Transfer-Encoding", "chunked");
postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8") ;
//参数设置,需要注意的就是里边不能传NULL,要传空字符串
org.apache.commons.httpclient.NameValuePair[] data = {
new org.apache.commons.httpclient.NameValuePair("topicName", topicName),
new org.apache.commons.httpclient.NameValuePair("message", message)
};
postMethod.setRequestBody(data);
org.apache.commons.httpclient.HttpClient httpClient = new org.apache.commons.httpclient.HttpClient();
int response = httpClient.executeMethod(postMethod); // 执行POST方法
result = postMethod.getResponseBodyAsString() ;
System.out.println(response);
System.out.println(result);
} catch (Exception e) {
// logger.info("请求异常"+e.getMessage(),e);
throw new RuntimeException(e.getMessage());
}
return result;
}
如何能够看到gzip压缩后数据的大小 对比正常发送压缩比例