Simple use of asihttprequest framework

Source: Internet
Author: User

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 );

}



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.