Data compression
Use gzip to process compressed response data
Starting with version 0.9, ASIHTTPRequest prompts the server that it can receive gzip-compressed data. Many Web servers can compress the data before it is sent-which speeds up download speed and reduces traffic usage, but it pays for the server's CPU (compressed data) and the client (decompression data). In general, only a certain number of data will be compressed-many binary format files like jpeg,gif,png,swf and PDFs have compressed their data, so the data is sent to the client without gzip compression. 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
The Apache 2.x version is already equipped with mod_deflate extensions, which allows Apache to transparently compress specific kinds of data. To turn on this feature, 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
-(Ibaction) Graburl: (ID) sender
{
Nsurl *url = [Nsurl urlwithstring:@ "http://www.dreamingwish.com"];
ASIHTTPRequest *request = [ASIHTTPRequest Requestwithurl:url];
The default is yes, you can set it to No to disable gzip compression
[Request Setallowcompressedresponse:yes];
[Request startsynchronous];
BOOL *datawascompressed = [request isresponsecompressed]; Has the response been compressed by gzip?
NSData *compressedresponse = [request Rawresponsedata]; Compressed data
NSData *uncompresseddata = [request ResponseData]; Uncompressed data
NSString *response = [request responsestring]; Extracted string
}
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 uncompressed 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 the request completes. If the Shouldwaittoinflatecompressedresponses property of the request is set to No,asihttprequest, the data received will be decompressed in real time. In some cases, this increases the speed slightly because the data can be processed while the reqeust is waiting for network data.
This feature can be useful if you need to stream the response data stream (for example, XML and JSON parsing). If this option is enabled, you can request:didreceivedata the returned network data to the parser by implementing the Proxy function: 1.1 points.
Note that if the shouldwaittoinflatecompressedresponses is set to No, the raw (uncompressed) data is discarded. Refer to ASIHTTPRequest.h's code comments for details.
Compress request data using gzip
The new feature of the 1.0.3 version is the GZIP compression request data. Using this feature, you can make your program compress post/put content by setting Shouldcompressrequestbody to Yes, the default value is No.
Apache Mod_deflate can automatically decompress the request body of gzip compression (with appropriate settings). This approach works for CGI content, but not for content-filter modules (such as mod PHP), in which case you have to decompress the data yourself.
ASIHTTPRequest cannot detect if a server can receive a compressed request body. Use this feature when you are sure that the server can extract the gzip package.
Avoid compressing a compressed format, such as jpeg/png/gif/pdf/swf, and you'll find that the compressed data is larger than the original data.
Use of cookies
Persistent cookies
ASIHTTPRequest allows you to use global storage to share cookies with all programs that use Cfnetwork or nsurlrequest interfaces.
If you set Usecookiepersistence to Yes (the default), cookies are stored in the shared Nshttpcookiestorage container and are automatically reused by other request. It is worth mentioning that ASIHTTPRequest sends cookies to the server that are created by other programs (if they are valid for a particular request).
You can empty all cookies created during the session:
[ASIHTTPRequest Setsessioncookies:nil];
The ' session cookie ' here refers to all the cookies created in the sessions, not the cookie with no expiration time (that is, a cookie that is usually referred to as a conversation, which is purged at the end of the program).
In addition, there is a handy function clearsession to clear all cookies and cached authorization data generated during session.
Handle Cookies Yourself
If you want, you can turn off usecookiepersistence to manage a series of cookies for a request:
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 value of the cookie named ' Asihttprequesttestcookie '
url = [Nsurl urlwithstring:@ "http://www.dreamingwish.com/"];
request = [ASIHTTPRequest Requestwithurl:url];
[Request Setusecookiepersistence:no];
[Request Setrequestcookies:[nsmutablearray Arraywithobject:cookie]];
[Request startsynchronous];
Will print: I have ' Test value ' as the ' Asihttprequesttestcookie '
NSLog (@ "%@", [request responsestring]);