[IOS:OC] Sending a network request instance using Nsurl

Source: Internet
Author: User

#import "ViewController.h" @interface Viewcontroller () <NSURLConnectionDelegate,NSURLConnectionDataDelegate> @property (nonatomic,strong) nsoperationqueue *queue;//queue @end@implementation Viewcontroller//conn when using conn asynchronous connection The default is to asynchronously use a GET request to get the response data using the Proxy method and error-(ibaction) Get: (UIButton *) Sender {//Create a request Nsurlrequest *request=[nsurlrequest    Requestwithurl:[nsurl urlwithstring:@ "Http://115.29.104.29/Server.asmx/JsonDemo1"];    Create connection Nsurlconnection *conn=[nsurlconnection connectionwithrequest:request delegate:self]; Start connection [conn start];} -(Ibaction) post: (ID) Sender {//post needs to be set using variable request Nsmutableurlrequest *request=[nsmutableurlrequest Requestwith    Url:[nsurl urlwithstring:@ "Http://115.29.104.29/Server.asmx/JsonDemo1"];    [Request sethttpmethod:@ "POST"];    Nsurlconnection *conn=[nsurlconnection connectionwithrequest:request delegate:self]; [conn start];} -(Ibaction) Sync: (ID) Sender {//synchronous GET request Using Conn static method Sendsync Nsurlrequest *reques=[nsurlrequest requesTwithurl:[nsurl urlwithstring:@ "Http://115.29.104.29/Server.asmx/JsonDemo1"];    Use parameters to get return error and response Nsurlresponse *res;    Nserror *error;    NSData *data=[nsurlconnection sendsynchronousrequest:reques returningresponse:&res error:&error];    if (Error) {NSLog (@ "error:%@", error.localizeddescription);    } else{NSLog (@ "%@", [[NSString Alloc]initwithdata:data encoding:nsutf8stringencoding]); }}-(ibaction) Async: (ID) Sender {//Asynchronous GET request Nsurlrequest *request=[nsurlrequest Requestwithurl:[nsurl urlwithstring:    @ "Http://115.29.104.29/Server.asmx/JsonDemo1"]; [Nsurlconnection sendasynchronousrequest:request queue:_queue completionhandler:^ (NSURLResponse *response, NSData * Data, Nserror *connectionerror) {//block if (connectionerror) {NSLog (@ "error:%@", Connectio        Nerror.localizeddescription);        } else{NSLog (@ "%@", [[NSString Alloc]initwithdata:data encoding:nsutf8stringencoding]); }    }];} -(VOID) Connection: (Nsurlconnection *) connection didreceivedata: (NSData *) data{NSLog (@ "%@", [[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding]);} -(void) connection: (Nsurlconnection *) connection didreceiveresponse: (Nsurlresponse *) response{NSLog (@ "server response");} -(void) connection: (Nsurlconnection *) connection didfailwitherror: (Nserror *) error{NSLog (@ "error:%@", error.localizeddescription);}    -(void) viewdidload {[Super viewdidload]; _queue=[[nsoperationqueue Alloc]init];} -(void) didreceivememorywarning {[Super didreceivememorywarning];} @end

[IOS:OC] Sending a network request instance using Nsurl

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.