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
}];
- 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
+ (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