iOS Network-nsurlsessiontask Simple Introduction

Source: Internet
Author: User

NSURLSessionTask也可以用来发送网络请求, 相对NSURLConnection, 推荐使用NSURLSessionTask.

a.NSURLSessionTask是一个抽象类,本身不能使用,只能使用它的子类B.nsurlsessiondatatask\nsurlsessionuploadtask\nsurlsessiondownloadtaskBasic Use (get/post) with NSURLConnection similar:
-(void)Get1{    //1. Create a request pathNsurl *url = [Nsurl urlwithstring:@"Http://120.25.226.186:32812/login?username=123&pwd=123&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: Completionhandlerblock data: Response Body Response: Response header error: Wrong message*/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. Start the execution[Datatask resume];}-(void) get2{//1. Create a request pathNsurl *url = [Nsurl urlwithstring:@"Http://120.25.226.186:32812/login?username=123&pwd=123&type=JSON"]; //3. Creating a Session ObjectNsurlsession *session =[Nsurlsession sharedsession]; //4. Create a task    /*first parameter: Request object second parameter: Completionhandlerblock data: Response Body Response: Response header error: Wrong message*/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. Start the execution[Datatask resume];}-(void) post{//1. Create a request pathNsurl *url = [Nsurl urlwithstring:@"Http://120.25.226.186:32812/login"]; //2. Create a Request objectNsmutableurlrequest *request =[Nsmutableurlrequest Requestwithurl:url]; Request. HttpMethod=@"POST"; Request. Httpbody= [@"Username=520it&pwd=123&type=json"datausingencoding:nsutf8stringencoding]; //3. Creating a Session ObjectNsurlsession *session =[Nsurlsession sharedsession]; //4. Create a task    /*first parameter: Request object second parameter: Completionhandlerblock data: Response Body Response: Response header error: Wrong message*/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. Start the execution[Datatask resume];}

iOS Network-nsurlsessiontask Simple Introduction

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.