Network Processing 2-asynchronous post requests and synchronization requests

Source: Internet
Author: User

One, asynchronous POST request

If the request path is Http://192.168.1.102:8080/MJServer/login, there are 2 request parameters:

    • Username: Hen
    • Pwd:123
1.POST Request Detail Analysis

To send a POST request in iOS, first understand some of the details of the POST request:

1> Unlike get requests, the request parameters for a POST request are not stitched behind the request path, but are sent to the server side in the form of the request body.

The 2> POST request needs to send two parts of the data to the server side:

* Request Body Content: All request parameters

Username=%e6%af%8d%e9%b8%a1&pwd=123

The Chinese parameter needs to be encoded, and the parameter "hen" is encoded as " %E6%AF%8D%E9%B8%A1 "

* Request header information: Request body length, type of request data

(This is the analog POST request in Firebug)

Content-length refers to the request body length, content-type refers to the request data type

2.POST Request Code Implementation
 1//Request Address 2 NSString *urlstring = @ "Http://192.168.1.102:8080/MJServer/login"; 3//Initialize a Nsurl object 4 nsurl *url = [NSU RL urlwithstring:urlstring]; 5 6//Initialize a request 7 nsmutableurlrequest *request = [Nsmutableurlrequest Requestwithurl:url]; 8//Set Request Method 9 requests. HttpMethod = @ "POST"; 10//60 second Request Timeout one request.timeoutinterval = 60;12 13//Stitching request parameter: NSString *params = @ "Username= hen &p Wd=123 "; 15//Encode a string and turn it into NSData object NSData *data = [params datausingencoding:nsutf8stringencoding];17//Set Request body . Httpbody = data;19 20//Set Request header information-request body length of nsstring *contentlength = [NSString stringwithformat:@ "%i", data.length];22 [requ EST setvalue:contentlength forhttpheaderfield:@ "content-length"];23//Set Request header information-request data type [requests setvalue:@] application/x-www-form-urlencoded "forhttpheaderfield:@" Content-type "];25 26//Initialize a connection nsurlconnection *conn = [ Nsurlconnection connectionwithrequest:request delegate:self];28//Start an asynchronous request [Conn start]; 

1> set the request body content on line 18

2> Request header information was set from line 21st to 24th

3> in fact, the 21st to 24th line of code can be omitted. As long as we set the request body on line 18th, the system will automatically add the corresponding request header information according to the request body content when sending the POST request.

Ii. other request methods for Nsurlconnection

In addition to the Start method, Nsurlconnection provides 2 static methods to help us send an HTTP request

1. Asynchronous requests
1//Request Address 2 NSString *urlstring = @ "http://192.168.1.102:8080/MJServer/login?username=123&pwd=123"; 3//Initialize a Nsurl object 4 nsurl *url = [Nsurl urlwithstring:urlstring]; 5  6//Initialize a request 7 nsurlrequest *request = [Nsurlrequest Requestwithurl:url]; 8  9//Initialize an operations queue of Nsoperationqueue *q Ueue = [[[Nsoperationqueue alloc] init] autorelease];11//Send an asynchronous request [Nsurlconnection Sendasynchronousrequest:request Queue:queue completionhandler:13 ^ (nsurlresponse *response, NSData *data, Nserror *error) {     //parse into string data     NSString *STR = [[[NSString Alloc] initwithdata:data encoding:nsutf8stringencoding] autorelease];16     NSLog (@ "%@", STR); 17}];

In line 12, call SendAsynchronousRequest:queue:completionHandler: Method sends an asynchronous HTTP request, this method receives 3 parameters

* The 1th parameter is a Nsurlrequest object that encapsulates a GET request

* The 3rd parameter is a block, when the server successfully returns the data will callback this block,block nsdata *data parameter is the data returned by the server

* The 2nd parameter is an operation queue nsoperationqueue, when the server returns data successfully, the system will put the 3rd parameter into the operation queue to execute the block

2. Sync requests

All that is said is asynchronous requests, which is also a recommended method of request. Nsurlconnection also provides a static method to send a synchronous request.

1//Request Address 2 NSString *urlstring = @ "http://192.168.1.102:8080/MJServer/login?username=123&pwd=123"; 3//Initialize a Nsurl object 4 nsurl *url = [Nsurl urlwithstring:urlstring]; 5  6//Initialize a request 7 nsurlrequest *request = [Nsurlrequest Requestwithurl:url]; 8  9//Send a sync request ten nsdata *data = [Nsur Lconnection sendsynchronousrequest:request Returningresponse:nil error:nil];11 12//Parse into string data NSString *str = [[[ NSString Alloc] Initwithdata:data encoding:nsutf8stringencoding] autorelease];14 NSLog (@ "%@", str);

The sendSynchronousRequest:returningResponse:error is called on line 10th: The method sends a synchronous request, which is a blocking method that returns all data after the server returns, The return value is a NSData object that holds all the data returned by the server

Network Processing 2-asynchronous post requests and synchronization 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.