IOS network programming-asihttprequest framework synchronizes requests

Source: Internet
Author: User

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.

Related Article

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.