Asihttprequest is an open-source project with strong HTTP access. Allows simple APIs to complete complex functions, such as asynchronous requests, queue requests, Gzip compression, cache, resumable data transfer, progress tracking, file uploading, and HTTP authentication. In the new version, we also added support for Object-C closure blocks, making our code more streamlined and flexible.
Five dependent libraries:
Cfnetwork
Systemcorfiguration
Mobilecoreservices
And libz, libxml2
The content of the asihttprequest library can be downloaded from the GitHub open-source network.
I. asihttprequest synchronization request
// Synchronous request
-(Void) Synchronous :( nsurl *) URL
{
Asihttprequest * request = [asihttprequestrequestwithurl: url];
[Requestsetrequestmethod: @ "get"];
[Request settimeoutseconds: 60];
// Set the Request Header
// [Request setrequestheaders: <# (nsmutabledictionary *) #>]
// Set cookies and save them to the local device. Set the logon password. You do not need to log on next time.
// [Request setrequestcookies: <# (nsmutablearray *) #>]
// Send a synchronization request
[Request startsynchronous];
Nserror * error = request. error;
If (error = nil)
{
// Requested data
Nsdata * Data = request. responsedata;
Uiimage * image = [uiimageimagewithdata: Data];
Self. Image = image;
}
Else
{
Nslog (@ "request network error: % @", error );
}
}
// Asynchronous request
-(Void) asynchronous :( nsurl *) URL
{
Asihttprequest * request = [asihttprequestrequestwithurl: url];
[Requestsetrequestmethod: @ "get"];
[Request settimeoutseconds: 60];
// 1. Use delegate
// Request. Delegate = self;
// 2. Use block
[Request setcompletionblock: ^ {
// Block for network request Completion call
Nsdata * Data = request. responsedata;
Self. Image = [uiimageimagewithdata: Data];
}];
[Requestsetfailedblock: ^ {
// Block for network request failure call
}];
//////////////////////////////////////// //////
// Send an asynchronous request
[Request startasynchronous];
}
# Pragma mark-asihttprequestdelegate
-(Void) requestfinished :( asihttprequest *) Request
{
Nsdata * Data = request. responsedata;
Self. Image = [uiimageimagewithdata: Data];
}
-(Void) requestfailed :( asihttprequest *) Request
{
Nslog (@ "% @", request. Error );
}