iOS Network Development Basics-How to send data to the server

Source: Internet
Author: User

There are three ways to send data to the server: nsurlrequest,nsmutableurlrequest,nsurlconnection

Nsurlrequest sending a synchronous or asynchronous request to the server

Example: How to send a GET Request

* The default is GET Request

1.URL

Nsurl *url = [Nsurl urlwithstring:@ "http://www.baidu.com"];

//2. Request

Nsurlrequest *request = [Nsurlrequest Requestwithurl:url];

//3. Send Request

[Nsurlconnection sendasynchronousrequest:request queue:[nsoperationqueue Mainqueue] completionHandler:^ ( Nsurlresponse *response, NSData *data, Nserror *connectionerror) {

}];


Second, Nsmutableurlrequest send the request to the server

How to send a POST Request

//1. Create a URL : Request Path

Nsurl *url = [Nsurl urlwithstring:@ "Http://xxxxxx/login"];


//2. Create a request

Nsmutableurlrequest *request = [Nsmutableurlrequest Requestwithurl:url];

// Set Request Method

request. HttpMethod = @ "POST";

// set the request body : Request Parameters

nsstring *param = [NSString stringWithFormat:@ "username=%@&pwd=%@", Usernametext, Pwdtext] ;

NSString-NSData

Request. Httpbody = [param datausingencoding:nsutf8stringencoding];


Iii. Nsurlconnection sending a request to the server

Use nsurlconnection basic steps to send a request

//1. Create an NS URL Object : Request Request Path

Nsurl *url = [Nsurl urlwithstring:@ "http://4234324/5345345"];


//2. Incoming Nsurl Create a Nsurlrequest object, set the request header and the request body

Nsurlrequest *request = [Nsurlrequest Requestwithurl:url];


//3. Send nsurlrequest using nsurlconnection Request

[Nsurlconnection sendasynchronousrequest:request queue:queue Completionhandler:

^ (Nsurlresponse *response, NSData *data, Nserror *connectionerror) {

//4. Processing the data returned by the server

}];

    1. Send a sync request

+ (nsdata *) Sendsynchronousrequest: (nsurlrequest*) Request Returningresponse: (nsurlresponse * *) Response error: (nserror* *) error;

2. Send asynchronous request---is divided into block callback and Nsurlconnectiondelegate proxy method according to different processing method of server return data

    • Block callback

+ (void) sendasynchronousrequest: (nsurlrequest*) Request

Queue: (nsoperationqueue*) queue

Completionhandler: ( void (^) (nsurlresponse* response, nsdata* data, nserror* connectionerror)) handler

    • Request: Requests that need to be sent
    • Queue: Generally use the home row, storage handler this task
    • Handler: When the request is complete, the block is automatically called

    • Proxy methods in the Nsurlconnectiondelegate protocol
called when a response to the server starts to be received

-(void) connection: (nsurlconnection*) connection didreceiveresponse: (nsurlresponse*) Response

called when the data returned by the server is received (the data returned by the server is larger when it is called multiple times)

-(void) connection: (nsurlconnection*) connection didreceivedata: (nsdata*) data;

called when the data returned by the server is fully received

-(void) connectiondidfinishloading: (nsurlconnection*) connection;

call when request is faulted (e.g. request timed out)

-(void) connection: (nsurlconnection*) connection didfailwitherror: (nserror*) error;


D. Send JSON to the server

1. Be sure to use the POST request

2. Set the request header:

[Request setvalue:@ "Application/json" forhttpheaderfield:@ "Content-type"];

3. Set the JSON data as the request body

iOS Network Development Basics-How to send data to the server

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.