iOS third-party class library ASIHTTPRequest

Source: Internet
Author: User

ASIHTTPRequest

ASIHTTPRequest is an open source project directly on Cfnetwork that provides a more convenient and powerful package for HTTP network transmissions than the official. Its features are as follows:
1, the downloaded data is saved directly to the memory or file system
2, provide API for direct commit (HTTP POST) file
3, can directly access and modify HTTP request and response header
4, easy to get upload and download progress information
5, asynchronous request and queue, automatic management of upload and download queue management machine
6. Support for authentication and authorization
7,cookie
8, request-and-response gzip
9, Proxy request

Here's a small example of two 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, for example, add a request header:
[Request addrequestheader:@ "name" value:@ "Jory Lee"];

health 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"];

to set an authorized account for http:
[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 our data acquisition process, 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, see Asihttp than the traditional post method of convenience

Post Usage
NSString *post = @ "Put the parameters to be passed here";
NSData *postdata = [post datausingencoding:nsasciistringencoding allowlossyconversion:yes];

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

Nsmutableurlrequest *request = [[[[Nsmutableurlrequest alloc] init] autorelease];
[Request Seturl:[nsurl urlwithstring:@ "requesting 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 could is not being 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);
}

By contrast, Asihttp does save a lot of code. For more information, please visit the official website http://allseeing-i.com/

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.