Gzip encoding on HTTP is a technology used to improve the performance of Web applications. Large-Traffic web sites often use gzip compression technology to reduce the file size and reduce the file size. One is to reduce the storage space, and the other is to transfer files over the network, this reduces the transmission time. After writing this blog, the author tested that MB of text data was transferred to the client through gzip and then changed to kb, which results in extremely high compression efficiency.
I. Server
The server can be compressed in two ways, one can be compressed by itself, but the second method is recommended, with printwriter as the output stream. The tool-type code is as follows:
/*** Determine whether the browser supports gzip compression * @ Param req * @ return Boolean value */public static Boolean isgzipsupport (httpservletrequest req) {string headencoding = req. getheader ("Accept-encoding"); If (headencoding = NULL | (headencoding. indexof ("gzip") =-1) {// the client does not support gzipreturn false;} else {// supports gzip compression return true ;}} /*** create a printwriter object output in GZIP format. If the browser does not support gzip, create a normal printwriter object, * @ Param req * @ Param resp * @ return * @ throws ioexception */public static printwriter creategzippw (httpservletrequest req, httpservletresponse resp) throws ioexception {printwriter PW = NULL; if (isgzipsupport (req) {// supports gzip compression PW = new printwriter (New gzipoutputstream (resp. getoutputstream (); // set the return type to gzipresp in the header. setheader ("content-encoding", "gzip");} else {// the client does not support gzippw = resp. getwriter ();} return PW ;}
The servlet code is as follows:
Public void dopost (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {response. setcharacterencoding ("UTF-8"); response. setheader ("content-encoding", "gzip"); string ret = "{\" contentlayer \ ": {\" title \ ": \" content layer \"}, \ "pagelink \": {\ "title \": \ "page Jump \" },\ "webbrowser \" :{\ "title \": \ "browser \"}, "+" \ "inlinepage \": {\ "title \": \ "embedded Page \"}, \ "videocomp \": {\ "title \": \ "video \"}, "+" \ "popbutton \": {\ "title \": \ "Content Switch \"}, \ "zoomingpic \": {\ "title \": \ "zoom in and out a large image \"}, "+" \ "rotate360 \" :{\ "title \": \ "360 degree rotation \"} "; printwriter PW = new printwriter (New gzipoutputstream (response. getoutputstream (); PW. write (RET); PW. close ();} public void doget (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {This. dopost (request, response );}
The data tracked in the agent software is as follows:
«VrÎÏ+IÍ+ñI¬L-R²ªV*É,ÉIU²R:rëÄÝMjuÓS}2ó²e/m>üìÌë«@òáINEùåŨú¬?pàØw¼g^Nf^*ÈTóoRï[¬àÔåc[ÁÖç8äç¡»nÿª7@¢òós3óÒ2UþºýèÏg÷Tå$¤ +r·¸ðäZh¤
The actual data is as follows:
{"Contentlayer": {"title": "content layer"}, "pagelink": {"title": "page Jump"}, "webbrowser": {"title ": "Browser"}, "inlinepage": {"title": "embedded page"}, "videocomp": {"title": "video"}, "popbutton ": {"title": "Content Switch"}, "zoomingpic": {"title": "zoom in or out"}, "rotate360": {"title ": "360-degree rotation "}}
Ii. Android Client
Get the httpclient code:
Private Static defaulthttpclient gethttpclient () {defaulthttpclient httpclient = new defaulthttpclient (); // set the connection timeout time. httpclient. getparams (). setparameter (httpconnectionparams. connection_timeout, timeout_connection); // sets the time-out for reading data. getparams (). setparameter (httpconnectionparams. so_timeout, timeout_socket); // sets the character set httpclient. getparams (). setparameter ("HTTP. protocol. content-charset ", utf_8); Return httpclient ;}
Httppost:
Private Static httppost gethttppost (string URL) {httppost = new httppost (URL); // set the request timeout value httppost. getparams (). setparameter (httpconnectionparams. so_timeout, timeout_socket); httppost. setheader ("connection", "keep-alive"); httppost. addheader ("Accept-encoding", "gzip"); Return httppost ;}
Access Network code:
public static InputStream http_post_return_byte(String url,Map<String, String> params) throws AppException {DefaultHttpClient httpclient = null;HttpPost post = null;HttpResponse response = null;StringBuilder sb = null;StringEntity stringEntity = null;try {httpclient = getHttpClient();post = getHttpPost(url);sb = new StringBuilder();if (params != null && !params.isEmpty()) {Logger.d("In http_post the url is get here");for (Entry<String, String> entry : params.entrySet()) {sb.append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue(),HTTP.UTF_8)).append("&");}sb.deleteCharAt(sb.lastIndexOf("&"));Logger.d("In http_post the url is " + url + " and params is "+ sb.toString());stringEntity = new StringEntity(sb.toString());stringEntity.setContentType("application/x-www-form-urlencoded");post.setEntity(stringEntity);}response = httpclient.execute(post);int statusCode = response.getStatusLine().getStatusCode();Logger.d("statusCode is " + statusCode);if (statusCode != HttpStatus.SC_OK) {throw AppException.http(statusCode);}InputStream is = response.getEntity().getContent();Header contentEncoding = response.getFirstHeader("Content-Encoding");if (contentEncoding != null&& contentEncoding.getValue().equalsIgnoreCase("gzip")) {is = new GZIPInputStream(new BufferedInputStream(is));}return is;} catch (ClientProtocolException e) {e.printStackTrace();throw AppException.http(e);} catch (IOException e) {e.printStackTrace();throw AppException.network(e);} finally {/* * if (!post.isAborted()) { * * post.abort(); } httpclient = null; */}}
Reprint please indicate the source http://blog.csdn.net/shimiso
Welcome to our technical exchange group: 173711587