Data compression problem in ASIHTTPRequest and gzip

Source: Internet
Author: User

A similar problem has occurred, Mark, later modifiedrecently using ASI found that some network callback data can not be normal JSON parsing, the Responseheaders header print out, only to find the company server part of the data added gzip compression transmission. The most recent simple solution is to set up a network request does not return compressed data, automatic decompression is OK. [request setAllowCompressedResponse :NO ];   ----------------------------------------------------------------- -----------------------------------------------------------------Using gzip to process compressed response data

Starting with version 0.9, ASIHTTPRequest will prompt the server to receive gzip-compressed data.

Many Web servers can compress the data before it is sent-which speeds up the download speed and reduces the amount of traffic used, but costs the server's CPU (compressed data) and the client (extracting data). In general, only certain types of data are compressed-many binary-formatted files like jpeg,gif,png,swf and PDFs have been compressed to their data, so sending this data to the client is not gzip compressed. text files such as Web pages and XML files are compressed because they typically have a large amount of data redundancy.

How to set up Apache mod_deflate to compress data using gzip

Apache 2.x or later has been equipped with mod_deflate extensions, which allows Apache to transparently compress certain kinds of data. To turn this feature on, you need to enable mod_deflate in the Apache configuration file. and add the Mod_deflate command to your virtual host configuration or to the. htaccess file.

Using gzip in ASIHTTPRequest
123456789101112 - (IBAction)grabURL:(id)sender{NSURL*url = [NSURLURLWithString:@"http://www.cccsee.cn"];ASIHTTPRequest *request = [ASIHTTPRequestrequestWithURL:url];// 默认为YES, 你可以设定它为NO来禁用gzip压缩[requestsetAllowCompressedResponse:YES];[requeststartSynchronous];BOOL*dataWasCompressed = [requestisResponseCompressed];// 响应是否被gzip压缩过?NSData*compressedResponse = [requestrawResponseData];// 压缩的数据NSData*uncompressedData = [request responseData];// 解压缩后的数据NSString*response = [requestresponseString];// 解压缩后的字符串}

When Allowcompressedresponse is set to Yes, ASIHTTPRequest adds a accept-encoding header to the request indicating that we can receive gzip-compressed data. If the response header contains a content-encoding header indicating that the data is compressed, then calling responsedata or responsestring will get the extracted data. You can also get raw uncompressed data by calling Rawresponsedata .

Real-time decompression of corresponding data

By default, ASIHTTPRequest will not decompress the returned data until request is complete. If you set the shouldwaittoinflatecompressedresponses property of request to No,asihttprequest, the received data will be decompressed in real time. In some cases, this will slightly increase the speed because the data can be processed while Reqeust waits for network data.

This feature is useful if you need to stream the response data stream, such as XML and JSON parsing. If this option is enabled, you can feed the returned network data 1.1 points to the parser by implementing the proxy function request:didreceivedata:

Note that if shouldwaittoinflatecompressedresponses is set to No, the raw (uncompressed) data is discarded. Please refer to the ASIHTTPRequest.h code comment for details.

Compress the request data using gzip

The new feature of the 1.0.3 version is the GZIP compression request data. With this feature, you can make your program compress post/put content by setting shouldcompressrequestbody to Yes, the default value is No.

Apache's mod_deflate can automatically decompress the Gzip compressed request body (via the appropriate settings). This method works for CGI content, but not for content filter-type modules (such as mod PHP), in which case you have to decompress the data yourself.

Data compression problem in ASIHTTPRequest and gzip

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.