In IOS development, we can use Nsurlconnection to implement Get/post requests
First, nsurlconnection three kinds of request way
1. Send a sync request (return NSData data)
[Nsurlconnection sendsynchronousrequest:request returningresponse:&response Error:nil];
Executes on the current thread, returning NSData data
2. Sending an asynchronous request (using block)
[Nsurlconnection sendasynchronousrequest:request queue:[nsoperationqueue Mainqueue] completionHandler:^ ( Nsurlresponse *response, NSData *data, Nserror *connectionerror) {
NSLog (@ "Return Data:%@", data);
}];
After the asynchronous thread executes, the request succeeds, and the returned data body is obtained in the block callback function.
3. Sending asynchronous requests (using proxies)
Nsurlconnection *connection = [Nsurlconnection alloc] init];
Connection.delegate = self;
After the asynchronous thread executes, the request succeeds, the method that invokes the proxy gets the corresponding data, this method is suitable for large file fragment download.
/**1. Response received from the server */-(void) connection: (Nsurlconnection *) connection didreceiveresponse: (Nsurlresponse *) response{ }/**2. Data received from the server */-(void) connection: (Nsurlconnection *) connection didreceivedata: (NSData *) data{}/**3. The server's data has been received */-(void) connectiondidfinishloading: (nsurlconnection *) connection{}/**4. Request Error */-(void) connection: ( Nsurlconnection *) connection didfailwitherror: (Nserror *) error{}
Second, GET request
1. If there are parameters, directly after the URL, with? Separated, and each parameter is separated by &.
The nsurlconnection does not need to contain the request line and the request header, the underlying and wrapped, and the default is the GET request. We simply load the specified URL resource, create the request, and send the request.
Sample program: Access Server Login interface
1. Resource string nsstring *urlstr = @ "http://127.0.0.1/login.php?name= Zhang San &password=1234"; If the string contains Chinese to transcode urlstr = [Urlstr stringbyaddingpercentescapesusingencoding:nsutf8stringencoding]; 2. Create a resource path nsurl *url = [Nsurl urlwithstring:urlstr]; 3. Create Request Nsurlrequest *request = [Nsurlrequest requestwithurl:url]; 4. Send Request [nsurlconnection sendasynchronousrequest:request queue:[nsoperationqueue Mainqueue] completionhandler:^ (Nsurlresponse *response, NSData *data, Nserror *connectionerror) { NSLog (@ "%@", data); }];
Second, POST request
With Nsurlconnection, because the default is a GET request, it is necessary to declare that the request method is post and that the request body needs to be encapsulated
//1. Resource string NSString *urlstr = @ "http://127.0.0.1/login.php" ; If the string contains chinese to transcode//urlstr = [Urlstr stringbyaddingpercentescapesusingencoding:nsutf8stringencoding]; 2. Set the request path Nsurl *url = [Nsurl urlwithstring:urlstr ]; 3. Create request Nsmutableurlrequest *request = [Nsmutableurlrequest Requestwithurl:url]; The default is GET request Request.timeoutinterval = 5; Set request time-out requests. HttpMethod = @ "POST"; Set to POST request
//4. Set the request body nsstring *param = [NSString stringwithformat:@ "name=%@&password=%@" , name, PASSW Ord];//name and password generally get request from the input box. Httpbody = [param datausingencoding:nsutf8stringencoding]; 5. Send request Nsoperationqueue *queue = [Nsoperationqueue mainqueue]; [Nsurlconnection sendasynchronousrequest:request queue:queue completionhandler:^ (NSURLResponse *response, NSData * Data, Nserror *connectionerror) {//Call NSLog at the end of the request (@ "Return data-%@" , ";"}];
Third, head request
In fact, the head request and get request is similar, except that the server only returns the response header, does not return the specific data, this can help us to explain how to get the file size after downloading the file
Nsurl *url = [Nsurl urlwithstring:@ "Http://127.0.0.1/images/xhr.png"]; Nsmutableurlrequest *request = [Nsmutableurlrequest requestwithurl:url]; Request. HttpMethod = @ "HEAD"; Nsurlresponse *response = nil; [Nsurlconnection sendasynchronousrequest:request queue:[nsoperationqueue Mainqueue] completionHandler:^ ( Nsurlresponse *response, NSData *data, Nserror *connectionerror) {
}];
In comparison, the POST request is more secure than the GET request, get requests all data exposed in the URL, post data encapsulated in the request body, but through Firefox can also see the POST request body, for Web server development, Firefox is a tool, highly recommended.
There is no need to worry about data security, which will explain how to encrypt the data later.
iOS Network Chapter 4-Implementing Get/post/head requests with Nsurlconnection