iOS Development ASIHTTPRequest sending requests

Source: Internet
Author: User

#pragmaMark-Sync Request-(void) sync{//1. Create a Request objectNsurl *url = [Nsurl urlwithstring:@"http://192.168.x.x:8080/login?username=123&pwd=123"]; ASIHTTPRequest*request =[ASIHTTPRequest Requestwithurl:url]; Request.timeoutseconds=Ten; //2. Sending the request[Request startsynchronous]; //3. Detecting the results returned by the server    if(Request.error) {//request error (e.g. timeout)NSLog (@"Request Error---%@", Request.error); } Else{//Request succeededNSLog (@"%d%@", Request.responsestatuscode, request.responsestatusmessage); //NSLog (@ "%d", request.responseData.length);nsdictionary*dict =[nsjsonserialization JSONObjectWithData:request.responseData options:nsjsonreadingmutableleaves Error:nil]; NSLog (@"%@", Dict); //NSLog (@ "%@", request.responsestring);    }}
#pragmaMark-Asynchronous request--set proxy-(void) async{//1. Create a Request objectNsurl *url = [Nsurl urlwithstring:@"http://192.168.x.x:8080/login?username=123&pwd=123"]; ASIHTTPRequest*request =[ASIHTTPRequest Requestwithurl:url]; Request.timeoutseconds=Ten; //Nsurl *url2 = [Nsurl urlwithstring:@]http://192.168.1.200: 8080/mjserver/video "];//asihttprequest *request2 = [ASIHTTPRequest requestwithurl:url2]; //2. Setting up the agentRequest.Delegate=Self ;//request2.delegate = self;[Request Setdidstartselector: @selector (start)]; //3. Sending the request[Request startasynchronous];//[Request2 startasynchronous];}- (void) start{NSLog (@"start--------");}#pragmaMark Proxy Method-(void) requestfinished: (ASIHTTPRequest *) request{NSLog (@"requestfinished----");//request.responsedata: All data returned by the server}- (void) requestfailed: (ASIHTTPRequest *) request{NSLog (@"requestfailed----");}- (void) requeststarted: (ASIHTTPRequest *) request{NSLog (@"requeststarted----");}- (voidRequest: (ASIHTTPRequest *) Request Didreceivedata: (NSData *) data{NSLog (@"didreceivedata----");}
#pragmaMark-Asynchronous Request--block-(void) async{//1. Create a Request objectNsurl *url = [Nsurl urlwithstring:@"http://192.168.x.x:8080/login?username=123&pwd=123"]; Self.request=[ASIHTTPRequest Requestwithurl:url]; Self.request.timeOutSeconds=Ten; //2. Set Block[Self.request setstartedblock:^{NSLog (@"setstartedblock----");        }]; [Self.request Setdatareceivedblock:^ (NSData *data) {NSLog (@"setdatareceivedblock----");        }]; [Self.request Setcompletionblock:^{NSLog (@"setcompletionblock----");    }]; //self.request.delegate = self; //3. Sending the request[Self.request startasynchronous]; //Nsurl *url2 = [Nsurl urlwithstring:@]http://192.168.x.x: 8080/"];//asihttprequest *request2 = [ASIHTTPRequest requestwithurl:url2];//[Request2 setcompletionblock:^{//        //    }];//    //[Request2 setdatareceivedblock:^ (NSData *data) {//        //    }];//[Request2 startasynchronous];}

POST request

#pragmaMark-post Request-(void) post2{//1. Create a Request objectNsurl *url = [Nsurl urlwithstring:@"Http://192.168.1.200:8080/MJServer/login"]; Asiformdatarequest*request =[Asiformdatarequest Requestwithurl:url]; //2. Add request parameters (parameters in the request body)[Request Setpostvalue:@"123"Forkey:@"username"]; [Request Setpostvalue:@"999"Forkey:@"pwd"]; [Request Setcompletionblock:^{NSLog (@"---request is complete .");        }]; //3. Sending the request[Request startasynchronous];}- (void) post{//1. Create a Request objectNsurl *url = [Nsurl urlwithstring:@"Http://192.168.1.200:8080/MJServer/login"]; Self.request=[ASIHTTPRequest Requestwithurl:url]; Self.request.timeOutSeconds=Ten; //Set Request MethodSelf.request.requestMethod =@"POST"; //set the request body[Self.request appendpostdata:[@"username=123&pwd=123"Datausingencoding:nsutf8stringencoding]];//[Self.request setPostLength:self.request.postBody.length];//[self.request addrequestheader:[nsstring stringwithformat:@ "%d", Self.request.postBody.length] value:@ " Content-length "];//[self.request addrequestheader:@ "application/x-www-form-urlencoded" value:@ "Content-type"]; //2. Sending the request[Self.request startasynchronous];}

iOS Development ASIHTTPRequest sending requests

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.