GET Request:
Add the username and password we want to the URL, send a request to the server, the server to verify that it (the database) has the same user name and password
-(void) Getrequest {//First step, create Urlnsurl *url = [nsurlurlwithstring:@] http://192.168.99.17:8888/loginDome/ Foregroundaction!login?username=admin&password=admin "];//second step, create a network request via URL nsurlrequest *request = [[ nsurlrequestalloc]initwithurl:urlcachepolicy:nsurlrequestuseprotocolcachepolicytimeoutinterval:10];// Nsurlrequest initialization Method first parameter: Request access path, second parameter: Cache protocol, third parameter: Network request time-out (seconds)//Where cache protocol is an enumeration type contains:// Nsurlrequestuseprotocolcachepolicy (Base policy)//nsurlrequestreloadignoringlocalcachedata (ignoring local cache)// Nsurlrequestreturncachedataelseload (use cache first, if there is no local cache, download from the original address)//nsurlrequestreturncachedatadontload (use local cache, never download, If there is no cache locally, the request fails, and this policy is mostly used for offline operations)//Nsurlrequestreloadignoringlocalandremotecachedata (ignoring any caching policies, whether local or remote, always re-downloaded from the original address) Nsurlrequestreloadrevalidatingcachedata (if the local cache is valid, do not download any other cases from the original address)//third step, connect the server NSData *received = [ NSURLConnectionsendSynchronousRequest:requestreturningResponse:nilerror:nil]; NSString *str = [[nsstringalloc]initwithdata:receivedencoding:nsutf8stringencoding]; NSLog (@ "%@", str);}
POST request
Create a dictionary, which is the user name and password entered by our user, add this dictionary to the data, add data to the request, so we send the request with parameters to the server.
The server receives the parameter, makes the judgment, if returns a thing correctly to us (QQ on the landing succeeds), is incorrect, returns the error message (Username/password error and so on).
-(void) postrequest{ //First step, create URL nsurl *url = [nsurlurlwithstring:@ "http://192.168.99.17:8888/loginDome/ Foregroundaction!login? "]; The second step, create the request nsmutableurlrequest *request = [[Nsmutableurlrequestalloc]initwithurl:urlcachepolicy: NSURLREQUESTUSEPROTOCOLCACHEPOLICYTIMEOUTINTERVAL:10]; [Request sethttpmethod:@ "POST"];//Setup requested by post, default to get nsdictionary *dic [email protected]{@ "UserName": @ " Willingseal "@" password ": @" 187550936 "}; NSData *data = [NSJSONSerializationdataWithJSONObject:dicoptions:NSJSONWritingPrettyPrintederror:nil]; NSString *jsonstr = [[NSString alloc]initwithdata:data encoding:nsutf8stringencoding]; [Request Sethttpbody:data]; Set the data for the request body //Third step, connect the server nsdata *received = [Nsurlconnectionsendsynchronousrequest: RequestreturningResponse:nilerror:nil]; NSString *STR1 = [[nsstringalloc]initwithdata:receivedencoding:nsutf8stringencoding]; NSLog (@ "%@", str1); }
IOS front-end and back-end interactive GET request POST request