客户端HttpClient4处理 Servlet Gzip后的内容

人生难得几回搏,此时不搏待何时。这篇文章主要讲述客户端HttpClient4处理 Servlet Gzip后的内容相关的知识,希望能为你提供帮助。
1、服务器端servlet代码:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
response.setHeader("Cache-Control", "no-cache");
response.setContentType("text/html; charset=UTF-8");

String str = "中文测试this is a test!";

if (isGzipSupport(request)) // 支持gzip
response.setHeader("Content-Encoding", "gzip");
OutputStream os = response.getOutputStream();
GZIPOutputStream gs = new GZIPOutputStream(os);
gs.write(str.getBytes("UTF-8")); // 解决中文乱码问题
gs.finish();
gs.close();
os.close();
else
PrintWriter out = response.getWriter();
out.write(str);
out.flush();
out.close();



/**
* 判断客户端是否要求进行gzip压缩处理
* @param request
* @return
*/
private boolean isGzipSupport(HttpServletRequest request)
String headEncoding = request.getHeader("accept-encoding");
return (headEncoding != null & & (headEncoding.indexOf("gzip") != -1));



【客户端HttpClient4处理 Servlet Gzip后的内容】 通过判断http的header中accept-encoding 是否为gzip(表明客户端支持gzip解压,服务端可以使用gzip进行数据压缩),对返回的数据进行gzip压缩。

部署后,浏览器上访问可以得到如下内容:
中文测试this is a test!



通过抓包可以发现:


浏览器默认发送的request请求中会携带aeecpt-encoding gzip信息(现代浏览器都支持gzip解压)


2、客户端代码:
package nc.edu.nuc.utils;

import java.io.IOException;
import java.nio.charset.Charset;

import org.apache.http.Header;
import org.apache.http.HeaderElement;
import org.apache.http.HttpEntity;
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponse;
import org.apache.http.HttpResponseInterceptor;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.GzipDecompressingEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;


@SuppressWarnings("deprecation")
public class HttpInterceptorUtil
private static DefaultHttpClient httpclient = new DefaultHttpClient();

static
httpclient.addRequestInterceptor(new HttpRequestInterceptor()
public void process(final HttpRequest request, final HttpContext arg1)
throws HttpException, IOException
if (!request.containsHeader("Accept-Encoding"))
request.addHeader("Accept-Encoding", "gzip");


);

/*httpclient.addResponseInterceptor(new HttpResponseInterceptor()
public void process(
final HttpResponse response,
final HttpContext context) throws HttpException, IOException
HttpEntity entity = response.getEntity();
Header ceheader = entity.getContentEncoding();
if (ceheader != null)
HeaderElement[] codecs = ceheader.getElements();
for (int i = 0; i < codecs.length; i++)
if (codecs[i].getName().equalsIgnoreCase("gzip"))
response.setEntity(
new GzipDecompressingEntity(response.getEntity()));
return;





); */



public static void main(String...strings) throws ClientProtocolException, IOException
HttpGet httpget = new HttpGet("http://localhost:8080/WebTest/SendAndGzipServlet");

// Execute HTTP request
System.out.println("executing request " + httpget.getURI());
HttpResponse response = httpclient.execute(httpget);

System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
System.out.println(response.getLastHeader("Content-Encoding"));
System.out.println(response.getLastHeader("Content-Length"));
System.out.println("----------------------------------------");

HttpEntity entity = response.getEntity();

if (entity != null)
String content = EntityUtils.toString(entity);
System.out.println(content);
System.out.println("----------------------------------------");
System.out.println("Uncompressed size: "+content.length());


// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();




1)httpClient支持拦截器,我们队client添加了request和response两个拦截器,request拦截器目的是在http header中添加content-encoding gzip信息,方便告诉服务端可以使用gzip压缩,客户端支持解压;response拦截器目的是对gzip数据进行解压。

2)如果把response拦截器注释掉,返回的数据就是乱码:
executing request http://localhost:8080/WebTest/SendAndGzipServlet
----------------------------------------
HTTP/1.1 200 OK
Content-Encoding: gzip
Content-Length: 46
----------------------------------------
?








    推荐阅读