In the asihttprequest framework, the HTTP request-related classes include asihttprequest and asiformdatarequest. The most common classes are asihttprequest and asiformdatarequest, which is a subclass of asihttprequest, which can send data similar to HTML forms, you can also upload data. The POST request method is used by default. You can also use other HTTP request methods. Both of them can be used for asynchronous or synchronous requests. Next we will introduce the simplest get synchronization request.
1. Implement get synchronization requests
Implement the get synchronization request and use the basic request class asihttprequest. We still use the mynotes application as an example. We only need to consider the implementation of the query function. The startrequest Method for modifying the masterviewcontroller. m of the master View Controller is as follows:
-(Void) startrequest {nsstring * strurl = [[nsstring alloc] initwithformat: @ "http: // iosbook3/mynotes/WebService. php? Email =%@ & type =%@ & Action =%@ ", @" <your iosbook1.com user email address> ", @" JSON ", @" Query "]; nsurl * url = [nsurl urlwithstring: [strurl urlencodedstring]; asihttprequest * request = [asihttprequest requestwithurl: url]; [Request startsynchronous]; nslog (@ "request completed ..."); Nserror * error = [Request Error]; If (! Error) {// nsstring * response = [Request responsestring]; nsdata * Data = [Request responsedata]; nsdictionary * resdict = [nsjsonserialization jsonobjectwithdata: dataoptions: Unknown error: Nil]; [self reloadview: resdict] ;}}
To use the asihttprequest class, you must introduce the header file asihttprequest. h.
2. Implement post synchronous requests
Requests that send post methods use the asiformdatarequest class for both synchronous and asynchronous requests, but they are slightly different. This section describes how to use the asiformdatarequest class to send the POST method for Synchronous requests. The startrequest Method for modifying master 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 completed ..."); Nserror * error = [Request Error]; If (! Error) {// nsstring * response = [Request responsestring]; nsdata * Data = [Request responsedata]; nsdictionary * resdict = [nsjsonserialization jsonobjectwithdata: dataoptions: Unknown error: Nil]; [self reloadview: resdict] ;}}
To use asiformdatarequest, You need to introduce the header file asiformdatarequest. h.
If you want to send other request methods except get and post, you can use the [request setrequestmethod: @ "put"] statement, where put is the request method.