iOS Summary: ASIHTTPRequest class Library send requests (synchronous and asynchronous requests)

Source: Internet
Author: User

1. sending an asynchronous request

1) Import the header file in. h

#import "ASIHTTPRequest.h"

2) set up the agent

Asihttprequestdelegate

3) Start asynchronous request with set agent, url--> request

-(BOOL) application: (uiapplication *) application didfinishlaunchingwithoptions: (nsdictionary *) Launchoptions

{

self. Window = [[UIWindow alloc] initwithframe: [[UIScreen mainscreen] bounds]];

self. Window. backgroundcolor = [Uicolor whitecolor];

Asynchronous Request

Url

Nsurl *url = [Nsurl urlwithstring:@ "http://www.baidu.com"];

send a request

ASIHTTPRequest *request = [asihttprequest requestwithurl: url];

// agent

Request. delegate = self ;

// start

[Request Startasynchronous];

[self. Window makekeyandvisible];

return YES;

}

3) If you get the returned text information, call the responsestring method,

If you get a binary file, such as a picture, a MP3 file, call the NSData method to get a NSData object

-(void) requestfinished: (asihttprequest *) Request

{

nsstring *response = [request responsestring];

NSLog (@ "%@", response);

NSData *data = [request responsedata];

NSLog (@ "%@", data);

}

-(void) requestfailed: (asihttprequest *) Request

{

nserror *error = [request error];

NSLog (@ "%@", error);

}

Output Result:

Text information is

Binary information is

2. Synchronous requests (similar to asynchronous requests)

-(BOOL) application: (uiapplication *) application didfinishlaunchingwithoptions: (nsdictionary *) Launchoptions

{

self. Window = [[UIWindow alloc] initwithframe: [[UIScreen mainscreen] bounds]];

Override point for customization after application launch.

self. Window. backgroundcolor = [Uicolor whitecolor];

Sync Request

Url

Nsurl *url = [Nsurl urlwithstring:@ "http://www.baidu.com"];

send a request

ASIHTTPRequest *request = [asihttprequest requestwithurl: url];

// agent

Request. delegate = self ;

// start

[Request Startsynchronous];

nserror *error = [request error];

if (!error) {

nsstring *response = [request responsestring];

NSLog (@ "%@", response);

}

[self. Window makekeyandvisible];

return YES;

}

 In general, an asynchronous request should be used preferentially, and when the ASIHTTPRequest synchronization request is used in the main thread, the application's interface is locked and no action can be made until the request is complete.

iOS Summary: ASIHTTPRequest class Library send requests (synchronous and asynchronous requests)

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.