/*
The flow of network requests:
1. Construct Nsurl Connection Address
2. Constructs the Nsurlrequest request object, which contains the request header and the request body information.
3. Construction Nsurlsessionconfiguration, optional
4. Constructing the Nsurlsession Session object
5. Create a request task
6. Send a network request
*/
1 //2 //VIEWCONTROLLER.M3 //01-nsurlsession Request Network4 //5 //6 7 #import "ViewController.h"8 9 @interfaceViewcontroller ()Ten One @end A - @implementationViewcontroller - the- (void) Viewdidload { - [Super Viewdidload]; - - } + --(Ibaction) Getaction: (ID) Sender { + A /* at the flow of network requests: - 1. Construct Nsurl Connection Address - 2. Constructs the Nsurlrequest request object, which contains the request header and the request body information. - 3. Construction nsurlsessionconfiguration, optional - 4. Constructing the Nsurlsession Session object - 5. Create a request task in 6. Send a network request - to */ + -Nsurl *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"]; the *Nsmutableurlrequest *request =[Nsmutableurlrequest Requestwithurl:url]; $ Panax NotoginsengRequest.timeoutinterval = the; -Request. HttpMethod =@"GET"; the //settings for the request header + //request.allhttpheaderfields = @{ A // the // }; + //Request setvalue:<# (Nullable NSString *) #> forhttpheaderfield:<# (nonnull nsstring *) #> - $Nsurlsession *session =[Nsurlsession sharedsession]; $ - -Nsurlsessiondatatask *task = [Session datataskwithrequest:request completionhandler:^ (NSData * _Nullable data, Nsurlresponse * _nullable response, Nserror *_nullable Error) { the //the network request is completed and the block that will be called back after the response data is obtained - //the data represents the fetch, response represents the request object, and error indicates the request is wrongWuyiNshttpurlresponse *httpresponse = (Nshttpurlresponse *) response; the //Get Status Code -NSLog (@"Statuscode:%li", Httpresponse.statuscode); Wu - //Get response Header AboutNsdictionary *responseheader =Httpresponse.allheaderfields; $NSLog (@"responseheader:%@", Responseheader); - -Nserror *jsonerror =Nil; - ANsdictionary *result = [nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingmutablecontainers Error: &Jsonerror]; + the if(!jsonerror) { - $ //Show our data, if you want to update the UI, you need to go back to the main thread to finish. theNSLog (@"%@", result); the the } the - in the }]; the About //send a network request the [Task resume]; the the + } - the-(Ibaction) Postaction: (ID) Sender {Bayi theNsurl *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"]; the -Nsmutableurlrequest *request =[Nsmutableurlrequest Requestwithurl:url]; - theRequest.timeoutinterval = the; theRequest. HttpMethod =@"POST"; the theNSString *bodystring =@"cinema_id=1533"; -NSData *bodydata =[bodystring datausingencoding:nsutf8stringencoding]; the //The request parameters of the POST request are added to the request body. theRequest. Httpbody =Bodydata; the 94Nsurlsession *session =[Nsurlsession sharedsession]; the the theNsurlsessiondatatask *task = [Session datataskwithrequest:request completionhandler:^ (NSData * _Nullable data, Nsurlresponse * _nullable response, Nserror *_nullable Error) {98 About -Nserror *jsonerror =Nil;101 102Nsdictionary *result = [nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingmutablecontainers Error: &Jsonerror];103 104 if(!jsonerror) { the 106 //Show our data, if you want to update the UI, you need to go back to the main thread to finish. 107NSLog (@"%@", result);108 109 } the 111 the }];113 the [Task resume]; the the }117 @end
Nsurlsession Request Network -01-Network