Use gzip to process compressed response data
From version 0.9, ASIHTTPRequest will prompt the server that can receive gzip compressed data. Many web servers can compress the data before the data is sent-this can speed up the download speed and reduce traffic usage, but will compress the server's cpu data) and extract data from the client) at a cost. In general, only a few types of data will be compressed-many binary files, such as jpeg, gif, png, swf, and pdf, have already compressed their data, therefore, gzip is not compressed when the data is sent to the client. Text files, such as web pages and xml files, are compressed because they usually have a large amount of data redundancy.
How to Set apache mod_deflate to use gzip to compress data
Apache 2.x and later versions are equipped with mod_deflate extensions, which allows apache to transparently compress specific types of data. To enable this feature, you must enable mod_deflate in the apache configuration file. Add the mod_deflate command to your VM configuration or. htaccess file.
Use gzip in ASIHTTPRequest
- -(IBAction) grabURL :( id) sender
- {
- NSURL * url = [NSURL URLWithString: @ "http://www.dreamingwish.com"];
- ASIHTTPRequest * request = [ASIHTTPRequest requestWithURL: url];
- // The default value is YES. You can set it to NO to disable gzip compression.
- [Request setAllowCompressedResponse: YES];
- [Request startSynchronous];
- BOOL * dataWasCompressed = [request isResponseCompressed]; // is the response compressed by gzip?
- NSData * compressedResponse = [request rawResponseData]; // compressed data
- NSData * uncompressedData = [request responseData]; // decompressed data
- NSString * response = [request responseString]; // The decompressed string
- }
When allowCompressedResponse is set to YES, ASIHTTPRequest adds an Accept-Encoding header to the request, indicating that we can receive data compressed by gzip. If the response header contains a Content-Encoding header that indicates that the data has been compressed, call responseData or responseString to obtain the decompressed data. You can also call rawResponseData to obtain the original uncompressed data.
Real-time Data Extraction
By default, ASIHTTPRequest will not extract the returned data until the request is complete. If the shouldWaitToInflateCompressedResponses attribute of the request is set to NO, ASIHTTPRequest decompress the received data in real time. In some cases, this will slightly increase the speed because data can be processed when reqeust waits for network data.
This feature is useful if you need to process the response data stream such as XML and JSON parsing. If this option is enabled, You can implement the proxy function request: didReceiveData: To feed the 1.1 point network data returned to the parser.
NOTE: If shouldWaitToInflateCompressedResponses is set to NO, the original undecompressed data will be discarded. For more information, see ASIHTTPRequest. h.
Use gzip to compress request data
The new feature of version 1.0.3 is gzip compression request data. With this feature, you can set shouldCompressRequestBody to YES to compress POST/PUT content in your program. The default value is NO.
Apache mod_deflate can automatically decompress the gzip compressed Request body through appropriate settings ). This method applies to CGI content, but not to content-filter-type modules such as mod PHP). In this case, you must decompress the data yourself.
ASIHTTPRequest cannot detect whether a server can receive compressed request bodies. This feature is used when you confirm that the server can decompress the gzip package.
Avoid compressing compressed formats such as jpeg, png, gif, pdf, and swf. You will find that the compressed data is larger than the original data. Mengwei: Because the compressed package has header information)