After Xcode 7.0, the HTTP link cannot be accessed directly (HTTPS), and the following item needs to be added to Info.plist for proper access.
The process of using Nsurlsession for network requests:
1. Construction Nsurl
2. Constructing the Nsurlrequest Request object
3. Constructing the Nsurlsession Session object
4. Create a network request task: Datatask, Downloadtask, Uploadtask
5. Send a network request
1.GET requests (get is typically used to get/query resource information )
- (void) httpget{//1. Construction NsurlNsurl *url = [Nsurl urlwithstring:@"Http://piao.163.com/m/cinema/list.html?app_id=1&mobileType=iPhone&ver=2.6&channel=appstore &deviceid=9e89cb6d-a62f-438c-8010-19278d46a8a6&apiver=6&city=110000"]; //2. Constructing the Nsurlrequest Request ObjectNsmutableurlrequest *request =[[Nsmutableurlrequest alloc] init]; //to set the request objectRequest. URL =URL; Request. HttpMethod=@"GET"; //3. Constructing nsurlsession Session objects (singleton)Nsurlsession *session =[Nsurlsession sharedsession]; //4. Create Datatask Network request task (Request Task 3 Datatask, Downloadtask, Uploadtask) /*(1) Completionhandler: The Block (2) that will be called back after the network request is completed response represents the response object, which can get the status of the current response, and the response header data (3) data represents the server's transmission, that is, the content of the response body (4) error indicates that a fault occurred during the request and can be debugged with it. */Nsurlsessiondatatask*datatask = [Session datataskwithrequest:request completionhandler:^ (NSData * _nullable data, Nsurlresponse * _Nullable R Esponse, Nserror *_nullable Error) {Nshttpurlresponse*httpresponse = (Nshttpurlresponse *) response; //Print response status code, 200 means the request is successful, other Baidu HTTP response status code to see the comparison can beNSLog (@"Response Status Code =%LD", Httpresponse.statuscode); //JSON data is returned here, JSON parsing of the response data IDresult =[nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingmutablecontainers Error:nil]; NSLog (@"data received:%@", result); }]; //5. Start sending a network request or resume a pending network request task[Datatask resume]; }
2.POST requests (post is typically used to update resource information, and in many cases the specified information needs to be added to the request body)
-(void) httppost{//1. Construction nsurl Nsurl *url = [Nsurl urlwithstring:@] Http://piao.163.com/m/cinema/schedule.html?app_id= 1&mobiletype=iphone&ver=2.6&channel=appstore&deviceid=9e89cb6d-a62f-438c-8010-19278d46a8a6 &apiver=6&city=110000 "]; 2. Construct the Nsurlrequest Request object Nsmutableurlrequest *request = [[Nsmutableurlrequest alloc] init]; Request. url = URL; Request. HttpMethod = @ "POST"; NSString *bodystring = @ "cinema_id=1533"; Request. Httpbody = [bodystring datausingencoding:nsutf8stringencoding]; 3. Construct Nsurlsession Session object (singleton) nsurlsession *session = [Nsurlsession sharedsession]; 4. Create Datatask Network request task Nsurlsessiondatatask *task = [Session datataskwithrequest:request completionhandler:^ (NSData * _N ullable data, Nsurlresponse * _nullable response, Nserror * _nullable error) {//Print response status code, 200 indicates successful request, other Baidu HTTP response status code pair Take care of it. Nshttpurlresponse *httpresponse = (Nshttpurlresponse *) response; NSLog (@ "Response Status code =%LD", Httpresponse.statuscode); Get the response body data for JSON parsing nserror *jsonerror = nil; ID result = [nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingmutablecontainers Error:&jsonError ]; NSLog (@ "%@", result); }]; [Task resume];}
Print results
Network Programming (i) send a Get POST request with Nsurlsession