Open Source Library: ASIHTTPRequest
ASIHTTPRequest Download URL: http://github.com/pokeb/asi-http-request
Dependent 5 library files: cfnetwork, systemconfiguration,mobilecoreservices, and Libz, LIBXML2
A class method is used to control the number of executions of a queue, but once a request is sent it is executed immediately and cannot be controlled logically.
[ASIHTTPRequest sharedqueue].maxconcurrentoperationcount = 1;
Initialization of the request queue
Self.queue = [Asinetworkqueue queue];
Number of concurrent executions
Self.queue.maxConcurrentOperationCount = 1;
let the queue go on, create a queue can have we control his logical order of execution
[Self.queue go];
sending an asynchronous request
-(void) Asynchronous: (nsurl *) URL
{
asihttprequest *request = [asihttprequest requestwithurl: url];
[Request Setrequestmethod:@ "GET"];
[Request Settimeoutseconds:];
// send asynchronous request Agent
//request.delegate = self;
Call block when request data is completed
[Request Setcompletionblock: ^{
nsdata *data = Request. ResponseData;
UIImage *image = [UIImage imagewithdata:d ATA];
self. Image = image;
if (request. Didusecachedresponse) {
NSLog(@ " to cache ");
}Else {
NSLog(@ " is coming to the network ");
}
}];
// request data failed call block
[Request Setfailedblock: ^{
NSLog(@ " request data failed ");
}];
//------------------------ set the cache policy -----------
nsstring *cachepath = [nshomedirectory()stringbyappendingpathcomponent:@ " Documents "];
asidownloadcache *cache = [[asidownloadcache alloc] init];
[Cache Setstoragepath: CachePath];
Cache. defaultcachepolicy = asionlyloadifnotcachedcachepolicy;
// This will erase the last storage at the time of each painting
Request. cachestoragepolicy = asicacheforsessiondurationcachestoragepolicy;
// Create a persistent cache here and call it directly from memory each time you want to call
//request.cachestoragepolicy = asicachepermanentlycachestoragepolicy;
//Request.downloadcache = cache;
// start asynchronous request
[Request startasynchronous];
// Add the request to the queue
//Handle to set the Get instance object
Appdelegate *appdelegate = [UIApplication sharedapplication].delegate;
[Appdelegate.queue Addoperation:request];
}
send a sync request
-(void) Synchronous: (nsurl *) URL
{
//ASIHTTPRequest *request = [ASIHTTPRequest requestwithurl:url];
asiformdatarequest *request = [asiformdatarequest requestwithurl: url];
[Request Setrequestmethod:@ "GET"];
[Request Settimeoutseconds:];
// Set the request header
//[request setrequestheaders:<# (nsmutabledictionary *) #>];
// set cookier
//[Request setrequestcookies:<# (Nsmutablearray *) #>];
// send a sync request
[Request startsynchronous];
// receive an error message
nserror *error = Request. Error;
if (Error = = Nil) {
nsdata *data = Request. ResponseData;
UIImage *image = [UIImage imagewithdata:d ATA];
self. Image = image;
}Else{
NSLog(@ " error sending sync request data :%@", error);
}
}
#pragma mark---asynchronous delegate
Completing an asynchronous request
-(void) requestfinished: (ASIHTTPRequest *) request
{
Get Data
NSData *data = Request.responsedata;
UIImage *image = [UIImage imagewithdata:data];
Self.image = image;
}
Asynchronous request failed
-(void) requestfailed: (ASIHTTPRequest *) request
{
Nserror *error = Request.error;
NSLog (@ "Asynchronous request failed:%@", error);
}
Other methods:
ASIHTTPRequest Use understanding