ASIHTTPRequest Data Compression for iOS development

Source: Internet
Author: User

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

 
 
  1. -(IBAction) grabURL :( id) sender
  2. {
  3. NSURL * url = [NSURL URLWithString: @ "http://www.dreamingwish.com"];
  4. ASIHTTPRequest * request = [ASIHTTPRequest requestWithURL: url];
  5. // The default value is YES. You can set it to NO to disable gzip compression.
  6. [Request setAllowCompressedResponse: YES];
  7. [Request startSynchronous];
  8. BOOL * dataWasCompressed = [request isResponseCompressed]; // is the response compressed by gzip?
  9. NSData * compressedResponse = [request rawResponseData]; // compressed data
  10. NSData * uncompressedData = [request responseData]; // decompressed data
  11. NSString * response = [request responseString]; // The decompressed string
  12. }

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.