#import "ViewController.h"@interfaceViewcontroller ()@end@implementationViewcontroller-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event{[self post];}-(void)Get{ //1. Determine the URLNsurl *url = [Nsurl urlwithstring:@"Http://120.25.226.186:32812/login?username=520it&pwd=520it&type=JSON"]; //2. Create a Request objectNsurlrequest *request =[Nsurlrequest Requestwithurl:url]; //3. Creating a Session ObjectNsurlsession *session =[Nsurlsession sharedsession]; //4. Create a task /*first parameter: Request object second parameter: Completionhandler call data after request completion: Response Body Information Response: Response header information error: Wrong message when request fails Waiting error has a value*/Nsurlsessiondatatask*datatask = [Session datataskwithrequest:request completionhandler:^ (NSData * _nullable data, Nsurlresponse * _Nullable R Esponse, Nserror *_nullable Error) { //6. Parsing DataNSLog (@"%@", [[NSString Alloc]initwithdata:data encoding:nsutf8stringencoding]); }]; //5. Execute Task[Datatask resume];}-(void) get2{//1. Determine the URLNsurl *url = [Nsurl urlwithstring:@"Http://120.25.226.186:32812/login?username=520it&pwd=520it&type=JSON"]; //2. Create a Request object//nsurlrequest *request =[nsurlrequest Requestwithurl:url]; //3. Creating a Session ObjectNsurlsession *session =[Nsurlsession sharedsession]; //4. Create a task /*first parameter: Request path second parameter: Completionhandler call data when the request is complete response: Response header information Error: Error message when the request fails Value Note: The Datataskwithurl internally automatically creates a Request object (GET) as a parameter of the request path*/Nsurlsessiondatatask*datatask = [Session Datataskwithurl:url completionhandler:^ (NSData * _nullable data, Nsurlresponse * _Nullable response, Nserror *_nullable Error) { //6. Parsing DataNSLog (@"%@", [[NSString Alloc]initwithdata:data encoding:nsutf8stringencoding]); }]; //5. Execute Task[Datatask resume];}-(void) post{//1. Determine the URLNsurl *url = [Nsurl urlwithstring:@"Http://120.25.226.186:32812/login"]; //2. Create a Request objectNsmutableurlrequest *request =[Nsmutableurlrequest Requestwithurl:url]; //2.1 Set the request method to postRequest. HttpMethod =@"POST"; //2.2 Setting the request bodyRequest. Httpbody = [@"Username=520it&pwd=520it&type=json"datausingencoding:nsutf8stringencoding]; //3. Creating a Session ObjectNsurlsession *session =[Nsurlsession sharedsession]; //4. Create a task /*first parameter: Request object second parameter: Completionhandler call!!! When the request is complete call data in a child thread: Response Body Information Response: Response header information error: Wrong message when request is lost The error has a value when you fail.*/Nsurlsessiondatatask*datatask = [Session datataskwithrequest:request completionhandler:^ (NSData * _nullable data, Nsurlresponse * _Nullable R Esponse, Nserror *_nullable Error) {NSLog (@"%@", [Nsthread CurrentThread]); //6. Parsing DataNSLog (@"%@", [[NSString Alloc]initwithdata:data encoding:nsutf8stringencoding]); }]; //5. Execute Task[Datatask resume];}@end
# # # # #2 basic use of nsurlsession
(1) Steps to use
Create a task using nsurlsession and then execute the task
(2) About task
A.nsurlsessiontask is an abstract class that cannot be used by itself and can only use its subclasses
B.nsurlsessiondatatask\nsurlsessionuploadtask\nsurlsessiondownloadtask
(3) Send a GET request
"' OBJC
1. Create an Nsurlsession object (you can get singleton objects)
Nsurlsession *session = [Nsurlsession sharedsession];
2. Create a task based on the Nsurlsession object
Nsurl *url = [Nsurl urlwithstring:@ "Http://120.25.226.186:32812/login?username=ss&pwd=ss&type=JSON"];
Nsurlrequest *request = [Nsurlrequest Requestwithurl:url];
Method parameter Description
/*
Note: The block is called in a child thread, and if you have to do some UI refresh after getting the data, you need to go back to the main thread refresh
First parameter: Request object to send
Block: Call Block when the request finishes receiving data from the server response
Block-nsdata: The response body of the request
Block-nsurlresponse: The response information for this request, the response header, the real type is Nshttpurlresponse
Block-nserroe: Request error message
*/
Nsurlsessiondatatask * Datatask = [session datataskwithrequest:request completionhandler:^ (NSData * __nullable data, NSU Rlresponse * __nullable response, Nserror * __nullable error) {
Get the response header information
Nshttpurlresponse *res = (Nshttpurlresponse *) response;
4. Parse the received response data
NSLog (@ "%@\n%@", [[NSString alloc]initwithdata:data Encoding:nsutf8stringencoding],res.allheaderfields);
}];
3. Execute task
Note: The task that you just created is suspended by default, and you need to call this method to start a task (perform a task)
[Datatask resume];
```
(4) The second way to send a GET request
"' OBJC
Note: The method internally wraps the URL object as a Nsurlrequest object (by default, a GET request)
Method parameter Description
/*
First parameter: The URL address of the sending request
Block: Call Block when the request finishes receiving data from the server response
Block-nsdata: The response body of the request
Block-nsurlresponse: The response information for this request, the response header, the real type is Nshttpurlresponse
Block-nserroe: Request error message
*/
-(Nullable Nsurlsessiondatatask *) Datataskwithurl: (nsurl *) URL Completionhandler: (void (^) (NSData * __nullable data, Nsurlresponse * __nullable response, Nserror * __nullable error)] Completionhandler;
```
(5) Send a POST request
"' OBJC
1. Create an Nsurlsession object (you can get singleton objects)
Nsurlsession *session = [Nsurlsession sharedsession];
2. Create a task based on the Nsurlsession object
Nsurl *url = [Nsurl urlwithstring:@ "Http://120.25.226.186:32812/login"];
Create a Request object, and this is the request method for post, put the parameters in the request body to pass
Nsmutableurlrequest *request = [Nsmutableurlrequest Requestwithurl:url];
Request. HttpMethod = @ "POST";
Request. Httpbody = [@ "Username=520it&pwd=520it&type=json" datausingencoding:nsutf8stringencoding];
Nsurlsessiondatatask *datatask = [Session datataskwithrequest:request completionhandler:^ (NSData * __nullable data, Nsurlresponse * __nullable response, Nserror * __nullable error) {
Get the response header information
Nshttpurlresponse *res = (Nshttpurlresponse *) response;
Parse the received response data
NSLog (@ "%@\n%@", [[NSString alloc]initwithdata:data Encoding:nsutf8stringencoding],res.allheaderfields);
}];
3. Execute task
Note: The task that you just created is suspended by default, and you need to call this method to start a task (perform a task)
[Datatask resume];
```
---
iOS Development Network Learning VII: nsurlsession Basic use of Get and post requests