The interface uses gzip for compression, which has many advantages. The company decided to use it.
I found a document: Click to open the link.
I visited the IOS developer just now and saw an article titled "gzip decompression on the client" explaining how to use gzip in HTTP requests on IOS, therefore, we have the urge to write about the use of gzip in Android HTTP requests. Let's talk less about it. Let's take a look at why Gzip is used. The gzip code 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. 1. How can I use gzip in an HTTP request? Generally, in an HTTP connection, not all receivers support the GZIP format. Therefore, based on the HTTP protocol, if you can process the GZIP format, if you want the server to return content in GZIP format, you must declare "Accept-encoding" as "gzip" in the HTTP request header. If the server can compress the content to GZIP format, the header returned by the server will set the "content-encoding" attribute value to gzip and compress the returned content to GZIP format. 2. How to Implement gzip in Android code? First, you must add a gzip-format declaration in the HTTP request. Different HTTP connection objects are set in different ways. We use httpget as an example: httprequest = newhttpget (URL); httprequest. addheader ("Accept-encoding", "gzip"); when the server returns, determine whether to decompress the gzip content based on the value of "content-encoding" in the response header, the android SDK has built-in gzipinputstream to help us extract the gzip stream to get a normal input stream. Httpentity = httpresponse. getentity (); inputstream is = httpentity. getcontent (); If (httpentity. getcontentencoding (). getvalue (). contains ("gzip") {is = new gzipinputstream (is);} OK, so we can implement it. Simple. Do you think something is wrong? Is this easy to use function implemented? That's right. If it wasn't written in tianchao, it would be enough, but in tianchao, we certainly wouldn't be so worried. China's mobile operators have made an innovative network called WAP. Under normal conditions, the WAP network can only access the WAP intranet, this is why all foreign applications cannot use the WAP access point. The WAP network brings us two difficulties: 1. How to access the Internet in the WAP network, 2. the WAP network router processes the data, which is strange, right, it is like the emails we send will be peeked at, not just by peeking, but it will also be modified. Many HTTP request headers will be filtered out, And gzip headers will also be included, when the vro filters out the gzip mark and decompress the content, our gzip will not work. How can I bypass the filtering of those routers? One simple method is that the server's return does not comply with the HTTP protocol, and the content is compressed using gzip, but does not return content-encoding = gzip, so there is no way for the router. In this case, our program must be modified, because we cannot determine whether content-encoding is used to decompress gzip. We can input the stream to read the first two bytes. If it is 0x8b1f, it is considered to be in GZIP format. However, inputstream cannot be used once it reads a stream. Although it has a reset method, it does not work for some reason. After the experiment, we chose to use bufferedinputstream for transfer, because it can be reset to locate the beginning of the stream. Bufferedinputstream Bis = new bufferedinputstream (is); bis. mark (2); // get the first two bytes byte [] header = new byte [2]; int result = bis. read (header); // reset the input stream to the starting position bis. reset (); // determine whether it is in GZIP format if (result! =-1 & getshort (header, 0) = 0x8b1f) {is = new gzipinputstream (bis);} else {// obtain the first two bytes is = bis ;} as for how to bypass WAP restrictions to access the Internet, please continue to pay attention to Android it. We will explain it later.
All the precautions and key code.