Nsurlconnection sending a GET request

Source: Internet
Author: User

1 //VIEWCONTROLLER.M2 //04-Mastering-nsurlconnection sending a GET request3 //4 //Created by Xiaomage on 16/2/22.5 //2016 little brother. All rights reserved.6 //7 8 #import "ViewController.h"9 Ten @interfaceViewcontroller () <NSURLConnectionDataDelegate> One /** Notes*/ A@property (nonatomic, strong) Nsmutabledata *Resultdata; - @end -  the @implementationViewcontroller -  - #pragmaMark---------------------- - #pragmaMark Lazy Loading +-(Nsmutabledata *) Resultdata - { +     if(_resultdata = =Nil) { A_resultdata =[Nsmutabledata data]; at     } -     return_resultdata; - } - #pragmaMark---------------------- - #pragmaMark Events --(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event in { -[SelfDelegate]; to } +  - /* the Request: Request Header (nsurlrequest default contains) + Request body (get not) * Response: Response header (real type--->nshttpurlresponse) + response body (data to parse) $  */Panax Notoginseng #pragmaMark---------------------- - #pragmaMark Methods the-(void) Sync + { A     /* the GET:http://120.25.226.186: 32812/login?username=123&pwd=456&type=json + protocol + host address + interface name +?+ parameter 1& parameter 2& parameter 3 - Post:http://120.25.226.186: 32812/login $ protocol + host address + interface name $      */ -     //GET, no request body -     //1. Determine the request path theNsurl *url = [Nsurl urlwithstring:@"Http://120.25.226.186:32812/login?username=520it&pwd=520it&type=JSON"]; -     Wuyi     //2. Create a Request object the     //The request header does not need to be set (the default request header) -     //Request Method---> Default to get WuNsurlrequest *request =[[Nsurlrequest Alloc]initwithurl:url]; -      About     //3. Sending the request $     //Real type: Nshttpurlresponse -Nshttpurlresponse *response =Nil; -     /* - first parameter: Request Object A Second Parameter: response header information + Third parameter: Error message the return value: Response Body -      */ $     //the method is blocked, that is, if the method is not finished, the code behind will not be executed theNSData *data = [Nsurlconnection sendsynchronousrequest:request returningresponse:&response Error:nil]; the      the     //4. Parsing Data---> Strings the     //nsutf8stringencoding -NSLog (@"%@", [[NSString Alloc]initwithdata:data encoding:nsutf8stringencoding]); in      theNSLog (@"%zd", Response.statuscode); the } About  the-(void) Async the { the     //1. Determine the request path +Nsurl *url = [Nsurl urlwithstring:@"Http://120.25.226.186:32812/login?username=520it&pwd=520it&type=JSON"]; -      the     //2. Create a Request objectBayi     //The request header does not need to be set (the default request header) theNsurlrequest *request =[[Nsurlrequest Alloc]initwithurl:url]; the      -     //3. Sending an asynchronous request -     /* the first parameter: Request Object the second parameter: queue determines the calling thread of the code block Completionhandler the third parameter: Completionhandler callback when the request completes (Success | failed) the Response: Response header - Data: Response body the connectionerror: Error message the      */ the[Nsurlconnection sendasynchronousrequest:request queue:[[nsoperationqueue Alloc]init] completionHandler:^ ( Nsurlresponse * _nullable response, NSData * _nullable data, Nserror *_nullable Connectionerror) {94          the         the         //4. Parsing Data theNSLog (@"%@", [[NSString Alloc]initwithdata:data encoding:nsutf8stringencoding]);98          AboutNshttpurlresponse *res = (Nshttpurlresponse *) response; -NSLog (@"%zd", Res.statuscode);101NSLog (@"%@", [Nsthread CurrentThread]);102     }];103 }104  the-(void)Delegate106 {107     //1. Determine the request path108Nsurl *url = [Nsurl urlwithstring:@"Http://120.25.226.186:32812/login?username=123&pwd=123&type=JSON"];109      the     //2. Create a Request object111Nsurlrequest *request =[Nsurlrequest Requestwithurl:url]; the     113     //3. Set up the agent to send the request the     //3.1 the     //[Nsurlconnection connectionwithrequest:request delegate:self]; the     117     //3.2118     //[[Nsurlconnection alloc]initwithrequest:request delegate:self];119      -     //3.3 Set the agent, the time to send the request to check the value of startimmediately121     //(startimmediately = = YES will send | startimmediately = = NO to call the Start method)122nsurlconnection * connect = [[Nsurlconnection alloc]initwithrequest:requestDelegate: Self startimmediately:no];123     124     //Call Start Method the [connect start];126     127 //[connect cancel];//Cancel - }129  the #pragmaMark----------------------131 #pragmaMark Nsurlconnectiondatadelegate the //1. Call when a server response is received133-(void) Connection: (Nsurlconnection *) connection didreceiveresponse: (Nsurlresponse *) Response134 {135NSLog (@"%s", __func__);136 }137 138 //2. Call when the server returns data, call multiple times139-(void) Connection: (Nsurlconnection *) connection didreceivedata: (NSData *) Data $ {141NSLog (@"%s", __func__);142     143     //Stitching Data144 [Self.resultdata Appenddata:data];145 }146 //3. Call when the request fails147-(void) Connection: (Nsurlconnection *) connection didfailwitherror: (Nserror *) Error148 {149NSLog (@"%s", __func__); Max }151  the //4. Call at the end of the request153-(void) Connectiondidfinishloading: (Nsurlconnection *) Connection154 {155NSLog (@"%s", __func__);156     157NSLog (@"%@", [[NSString Alloc]initwithdata:self.resultdata encoding:nsutf8stringencoding]);158 }159 @end
View Code

Nsurlconnection sending a GET request

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.