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)