Introduction to powerful HTTP packaging open-source project asihttprequest

Source: Internet
Author: User
Introduction to powerful HTTP packaging open-source project asihttprequest

Asihttprequest is an open-source project directly on the cfnetwork. It provides an encapsulation of HTTP network transmission that is more convenient and powerful than the official website. Its features are as follows: 1. The downloaded data is directly stored in the memory or file system. 2.

Asihttprequest is an open-source project directly on the cfnetwork. It provides an encapsulation of HTTP network transmission that is more convenient and powerful than the official website. It has the following features:
1. The downloaded data is saved directly to the memory or file system.
2. provides APIs for directly submitting (http post) files
3. You can directly access and modify HTTP request and response headers.
4. Easy access to upload and download progress information
5. asynchronous requests and queues for automatic management of upload and download queue management machines
6. authentication and authorization support
7. Cookie
8. gzip of the request and response
9. proxy request

Here are two small examples of asihttprequest:

Nsurl * url = [nsurl urlwithstring: @ "http://www.baidu.com"];
Asihttprequest * request = [asihttprequest requestwithurl: url];
[Request start];
Nserror * error = [Request Error];
If (! Error ){
Nsstring * response = [Request responsestring];
}

When you need to add more request information, such as adding a request header:

[Request addrequestheader: @ "name" value: @ "Jory Lee"];

Key value when adding a POST request:

[Request setpostvalue: @ "Ben" forkey: @ "first_name"];
[Request setpostvalue: @ "copsey" forkey: @ "last_name"];
[Request setfile: @ "/users/BEN/desktop/ben.jpg" forkey: @ "photo"];

Set the HTTP Authorization account:

[Request setusername: @ "username"];
[Request setpassword: @ "password"];

An asynchronous request:

-(Ibaction) graburlinbackground :( ID) sender
{
Nsurl * url = [nsurl urlwithstring: @ "http://allseeing-i.com"];
Asihttprequest * request = [asihttprequest requestwithurl: url];
[Request setdelegate: Self];
[Request startasynchronous];
}

-(Void) requestfinished :( asihttprequest *) Request
{
// Use when fetching text data
Nsstring * responsestring = [Request responsestring];

// Use when fetching binary data
Nsdata * responsedata = [Request responsedata];
}

-(Void) requestfailed :( asihttprequest *) Request
{
Nserror * error = [Request Error];
}

In the process of data acquisition, if the data source is complex, a request queue is essential:

-(Ibaction) graburlinthebackground :( ID) sender
{
If (! [Self queue]) {
[Self setqueue: [[nsoperationqueue alloc] init] autorelease];
}

Nsurl * url = [nsurl urlwithstring: @ "http://allseeing-i.com"];
Asihttprequest * request = [asihttprequest requestwithurl: url];
[Request setdelegate: Self];
[Request setdidfinishselector: @ selector (requestdone :)];
[Request setdidfailselector: @ selector (requestwentwrong :)];
[[Self queue] addoperation: request]; // queue is an nsoperationqueue
}

-(Void) requestdone :( asihttprequest *) Request
{
Nsstring * response = [Request responsestring];
}

-(Void) requestwentwrong :( asihttprequest *) Request
{
Nserror * error = [Request Error];
}

In addition, you can use the following code example to check the convenience of asihttp over the traditional post method.

Post usage


Nsstring * post = @ "Here the parameters to be passed ";
Nsdata * postdata = [Post datausingencoding: nsasciistringencoding allowlossyconversion: Yes];

Nsstring * postlength = [nsstring stringwithformat: @ "% d", [postdata length];

Nsmutableurlrequest * request = [[nsmutableurlrequest alloc] init] autorelease];
[Request seturl: [nsurl urlwithstring: @ "request address"];
[Request sethttpmethod: @ "Post"];
[Request setvalue: postlength forhttpheaderfield: @ "Content-Length"];
// [Request addvalue: @ "gzip" forhttpheaderfield: @ "accepts-encoding"];
[Request setvalue: @ "application/X-WWW-form-urlencoded" forhttpheaderfield: @ "Content-Type"];
[Request sethttpbody: postdata];

Nsurlconnection * conn = [[nsurlconnection alloc] initwithrequest: Request delegate: Self];

If (conn)
{
Receivedbdata = [[nsmutabledata data] retain];
}
Else
{
// Inform the user that the download cocould not be made
}

Asihttp Method

Nsurl * url = [nsurl urlwithstring: @ "request address"];
// Asihttprequest * request = [asihttprequest requestwithurl: url];
Asiformdatarequest * request = [asiformdatarequest requestwithurl: url];
[Request setpostvalue: @ "value" forkey: @ "parameter 1"];
[Request setpostvalue: @ "value" forkey: @ "parameter 2"];

[Request start];
Nserror * error = [Request Error];
If (! Error ){
Nsstring * response = [Request responsestring];
Nslog (response );
}

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.