GET request
#pragma mark-get Login-(void) getlogon{//1. URL nsstring *urlstr = [NSString stringwithformat:@ "http://localhost/login.php?username=%@&password=%@", Self.userName.text, Self.userPwd.text]; Nsurl *url = [Nsurl urlwithstring:urlstr]; 2. Request nsurlrequest *request = [Nsurlrequest Requestwithurl:url]; 3. Connection//1> cannot do follow-up work until login is completed! 2> login In progress, you can allow users to do something else will be better! 3> allows the login operation to be done in other threads, does not block the main thread's work//4> conclusion: Login is also asynchronous access, the middle need to block [nsurlconnection sendasynchronousrequest:request que Ue:[[nsoperationqueue alloc] init] completionhandler:^ (nsurlresponse *response, NSData *data, Nserror *connectionError {if (Connectionerror = = nil) {//The network request is executed after the end! Convert data to string nsstring *str = [[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding]; num = 2 NSLog (@ "%@%@", str, [Nsthread CurrentThread]); Update interface [[NsoperaTionqueue Mainqueue] addoperationwithblock:^{self.logonResult.text = @ "Login complete"; }]; } }]; num = 1 NSLog (@ "Come here%@", [Nsthread CurrentThread]); Nsurlresponse *response = nil; 1. &response really understand? 2. Error: Why is null, and not nil//null is the c language = 0//In C, if the address of the pointer to 0 is not dangerous//nil is OC, is an empty object send message will not problem//[Response M Imetype]; [Nsurlconnection sendsynchronousrequest:request returningresponse:&response Error:NULL];}
POST request
#pragma mark-post Login-(void) postlogon{//1. URL Nsurl *url = [Nsurl urlwithstring:@ "http://localhost/login.php"]; 2. Request (can be changed request) nsmutableurlrequest *request = [Nsmutableurlrequest Requestwithurl:url]; // ? The POST//default is GET request requests. HttpMethod = @ "POST"; // ? Data body NSString *str = [NSString stringwithformat:@ "username=%@&password=%@", Self.userName.text, Self.userPwd.text] ; Converts a string into a data request. Httpbody = [Str datausingencoding:nsutf8stringencoding]; 3. Connect, async [nsurlconnection sendasynchronousrequest:request queue:[[nsoperationqueue alloc] init] completionhandler:^ ( Nsurlresponse *response, NSData *data, Nserror *connectionerror) {if (Connectionerror = = nil) { Execute after the network request is over! Convert data to string nsstring *str = [[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding]; num = 2 NSLog (@ "%@%@", str, [Nsthread CurrentThread]); Update interface [[Nsoperationqueue mainqueue] addoperationwithblock:^{self.logonResult.text = str; }]; } }]; num = 1 NSLog (@ "Come here%@", [Nsthread CurrentThread]);}
There are two ways to use Nsurlconnection: the first, as above, the second implementation of the Nsurlconnectiondatadelegate proxy
-(void) getlogon{//1. URL nsstring *urlstr = [NSString stringwithformat:@ "http://localhost/login.php?username=%@&password=%@", Self.userName.text, Self.mypwd]; NSLog (@ "%@", self.mypwd); Nsurl *url = [Nsurl urlwithstring:urlstr]; 2. Request nsurlrequest *request = [Nsurlrequest Requestwithurl:url]; 3. Connection, already more than 10 years old//is a very old technology nsurlconnection *connection = [nsurlconnection connectionwithrequest:request delegate:self]; Start working, in many multithreaded technologies, start run Dispatch_async (Dispatch_queue_create ("demo", Dispatch_queue_concurrent), ^{ [Connection start]; });} #pragma mark-nsurlconnectiondatadelegate proxy method #pragma mark accepts a response-(void) connection: (Nsurlconnection *) connection Didreceiveresponse: (Nsurlresponse *) response{//preparation work//button Click there will be a network request, in order to avoid duplication of space if (!self.data) {Self.dat A = [Nsmutabledata data]; } else {[Self.data setdata:nil]; }} #pragma mark receives the data, and if the data is large, such as video, it is called multiple times-(void) connection: (Nsurlconnection *) connection didreceivedata: (NSData *) data{//splicing data, the embodiment position of the binary stream [Self.data appenddata:data];} #pragma mark receives the completion, does the final processing work-(void) connectiondidfinishloading: (nsurlconnection *) connection{//FINAL processing nsstring *STR = [[NSString Alloc] InitWithData:self.data encoding:nsutf8stringencoding]; NSLog (@ "%@%@", str, [Nsthread CurrentThread]);} #pragma mark error handling, the network error probability is very high-(void) connection: (Nsurlconnection *) connection didfailwitherror: (Nserror *) error{ NSLog (@ "%@", error.localizeddescription);}Note: The update UI should be updated on the main thread for the reason that threads are safe
Update interface [[Nsoperationqueue mainqueue] addoperationwithblock:^{ self.logonResult.text = str; }];