Use of ASIHTTPRequest data compression and cookies for iOS development

Source: Internet
Author: User
Tags send cookies

Use of ASIHTTPRequest data compression and cookies for iOS development

Data Compression

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 it will make the server's cpu (compress data) and client (decompress data) pay the price. 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 is applicable 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.

Cookie usage

Persistent cookie

ASIHTTPRequest allows you to use global storage to share cookies with all programs that use the CFNetwork or NSURLRequest interface.

If useCookiePersistence is set to YES (default), the cookie is stored in the shared NSHTTPCookieStorage container and is automatically reused by other requests. It is worth mentioning that ASIHTTPRequest will send cookies created by other programs to the server (if these cookies are valid for specific requests ).

You can clear all the cookies created during the session:

[ASIHTTPRequest setSessionCookies: nil];

Here, 'session cookies 'refer to all the cookies created in a session, rather than those with no expiration time (that is, the session cookie, this cookie will be cleared at the end of the program ).

In addition, there is a convenient function clearSession to clear all cookies generated during the session and cache authorization data.

Process cookies by yourself

If you want to, you can close useCookiePersistence and manage a series of cookies for a request by yourself:

// Create a cookie

NSDictionary * properties = [[NSMutableDictionary alloc] init] autorelease];

[Properties setValue: [@ "Test Value" encodedCookieValue] forKey: NSHTTPCookieValue];

[Properties setValue: @ "ASIHTTPRequestTestCookie" forKey: NSHTTPCookieName];

[Properties setValue: @ ".dreamingwish.com" forKey: NSHTTPCookieDomain];

[Properties setValue: [NSDate dateWithTimeIntervalSinceNow: 60*60] forKey: NSHTTPCookieExpires];

[Properties setValue: @ "/asi-http-request/tests" forKey: NSHTTPCookiePath];

NSHTTPCookie * cookie = [[NSHTTPCookie alloc] initWithProperties: properties] autorelease];

// This url returns the cookie value named 'asihttprequesttestcookie '.

Url = [NSURL URLWithString: @ "http://www.dreamingwish.com/"];

Request = [ASIHTTPRequest requestWithURL: url];

[Request setUseCookiePersistence: NO];

[Request setRequestCookies: [NSMutableArray arrayWithObject: cookie];

[Request startSynchronous];

// The output will be: I have 'test value' as the Value of 'asihttprequesttestcookie'

NSLog (@ "% @", [request responseString]);

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.