1#import"ViewController.h"2 3 @interface Viewcontroller ()4 5 @end6 7 @implementation Viewcontroller8-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event9 {Ten [self post]; One } A --(void) Post - { the //1. Determine the request path -Nsurl *url = [Nsurl urlwithstring:@"Http://120.25.226.186:32812/login"]; - - //2. Create a mutable Request object +Nsmutableurlrequest *request =[Nsmutableurlrequest Requestwithurl:url]; - + //3. Modify the request method, post must be uppercase ARequest. HttpMethod =@"POST"; at - //set properties, request timed out -Request.timeoutinterval =Ten; - - //set the request header User-agent - //Note: Key must be consistent (used to pass data to the background) in[Request SetValue:@"iOS 10.1"Forhttpheaderfield:@"user-agent"]; - to //4. Set the request body information, string--->nsdata +Request. Httpbody = [@"Username=520it&pwd=123&type=json"datausingencoding:nsutf8stringencoding]; - the //5. Sending the request *[Nsurlconnection sendasynchronousrequest:request queue:[nsoperationqueue Mainqueue] completionHandler:^ ( Nsurlresponse * _nullable response, NSData * _nullable data, Nserror *_nullable Connectionerror) { $ Panax Notoginseng //6. Parsing data, NSData--->nsstring -NSLog (@"%@", [[NSString Alloc]initwithdata:data encoding:nsutf8stringencoding]); the }]; + } A the@end
OC-Network Request-Nsurlconnection-post