The classes associated with HTTP requests in the ASIHTTPRequest framework are: ASIHTTPRequest and asiformdatarequest, the most common of which is asihttprequest, Asiformdatarequest is a subclass of ASIHTTPRequest, Asiformdatarequest can send similar HTML form data, or upload data, by default the POST request method. Other HTTP request methods are also available. All of them can be asynchronous or synchronous requests. Below we introduce from the simplest get sync request.
1. Implement Get sync Request
Implementing a Get synchronization request uses the most basic request Class asihttprequest. We still use the Mynotes application as an example, only consider the query function implementation, modify the main view controller masterviewcontroller.m Startrequest method is as follows:
-(void) startrequest
{
NSString *strurl = [[NSString alloc] Initwithformat:
@ "http://iosbook3/mynotes/ webservice.php?email=%@&type=%@&action=%@ ",
@" < your iosbook1.com user email > ", @" JSON ", @" Query "];
Nsurl *url = [Nsurl urlwithstring:[strurl urlencodedstring]];
ASIHTTPRequest *request = [ASIHTTPRequest requestwithurl:url];
[Request startsynchronous];
NSLog (@ "request complete ...");
Nserror *error = [request ERROR];
if (!error) {
//nsstring *response = [request responsestring];
NSData *data = [request ResponseData];
Nsdictionary *resdict = [nsjsonserialization jsonobjectwithdata:data
options:nsjsonreadingallowfragments Error : nil];
[Self reloadview:resdict];
}
Using the ASIHTTPRequest class, you need to introduce header file ASIHTTPRequest.h.
2, the implementation of post synchronization request
The
sends a request for a post method, both synchronous and asynchronous, using the Asiformdatarequest class, except that they are slightly different. In this section we only describe the use of the Asiformdatarequest class to send post methods under synchronization requests. The Startrequest method for modifying the main view controller MASTERVIEWCONTROLLER.M is as follows:
-(void) startrequest
{
NSString *strurl = @ "http://iosbook3/mynotes/webservice.php";
Nsurl *url = [Nsurl urlwithstring:[strurl urlencodedstring]];
Asiformdatarequest *request = [Asiformdatarequest requestwithurl:url];
[Request setpostvalue:@ "< your iosbook1.com user email >" forkey:@ "email"]; ③
[Request setpostvalue:@ ' JSON ' forkey:@ ' type '];
[Request setpostvalue:@ "query" forkey:@ "action"];
[Request startsynchronous];
NSLog (@ "request complete ...");
Nserror *error = [request ERROR];
if (!error) {
//nsstring *response = [request responsestring];
NSData *data = [request ResponseData];
Nsdictionary *resdict = [nsjsonserialization jsonobjectwithdata:data
options:nsjsonreadingallowfragments Error : nil];
[Self reloadview:resdict];
}
The use of Asiformdatarequest requires the introduction of header file ASIFormDataRequest.h.
If we want to send a request method other than Get and post, you can use the [request setrequestmethod:@ ' put] statement, where put is the request method.
This article is from the "Dongsheng" blog, please be sure to keep this source http://2009315319.blog.51cto.com/701759/1168240