First, I will introduce to you the meanings represented by synchronous requests, asynchronous requests, GET requests, and POST in iOS, and then introduce them to you through examples one by one.
1. The synchronization request can request data from the Internet. Once the synchronization request is sent, the program will stop user interaction until the server returns the data and can only perform the next operation.
2. Asynchronous requests will not block the main thread, but will create a new thread to operate. After the user issues an asynchronous request, he can still operate the UI and the program can continue to run.
3. GET request, write the parameters directly on the access path. The operation is simple, but it is easy to be seen by the outside world. It is not very safe and has a maximum address of 255 bytes;
4. POST request, put the parameters into the body. The POST request operation is relatively complex, and requires separation of parameters and addresses, but it is highly secure. The parameters are placed in the body and are not easily captured.
1. Synchronize GET requests
//Step 1, create a URLNSURL *url = [NSURL URLWithString:@"/?type=focus-c"]; //Step 2: Create a network request through the URLNSURLRequest *request = [[NSURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10]; //NSURLRequest initialization method The first parameter: the request access path, the second parameter: the cache protocol, the third parameter: the network request timeout time (seconds)The cache protocol is an enumeration type.: NSURLRequestUseProtocolCachePolicy(Basic Strategy) NSURLRequestReloadIgnoringLocalCacheData(Ignore local cache) NSURLRequestReturnCacheDataElseLoad(Use cache first,If there is no local cache,Downloaded from the original address) NSURLRequestReturnCacheDataDontLoad(Using local cache,Never download,If there is no cache locally,The request failed,This strategy is mostly used for offline operations) NSURLRequestReloadIgnoringLocalAndRemoteCacheData(Ignore any cache policies,Whether local or remote,Always re-download from the original address) NSURLRequestReloadRevalidatingCacheData(If the local cache is valid, no download,Any other situations will be downloaded from the original address) //Step 3: Connect to the serverNSData *received = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; NSString *str = [[NSString alloc]initWithData:received encoding:NSUTF8StringEncoding]; NSLog(@"%@",str);
2. Synchronize POST requests
//Step 1, create a URLNSURL *url = [NSURL URLWithString:@"/"]; //Step 2, create a requestNSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10]; [request setHTTPMethod:@"POST"];//Set the request method to POST, default to GETNSString *str = @"type=focus-c";//Set parametersNSData *data = [str dataUsingEncoding:NSUTF8StringEncoding]; [request setHTTPBody:data]; //Step 3: Connect to the serverNSData *received = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; NSString *str1 = [[NSString alloc]initWithData:received encoding:NSUTF8StringEncoding]; NSLog(@"%@",str1);
3. Asynchronous GET request
//Step 1, create urlNSURL *url = [NSURL URLWithString:@"/?type=focus-c"]; //Step 2, create a requestNSURLRequest *request = [[NSURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10]; //Step 3: Connect to the serverNSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
4. Asynchronous POST request
//Step 1, create urlNSURL *url = [NSURL URLWithString:@"/"]; //Step 2, create a requestNSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10]; [request setHTTPMethod:@"POST"]; NSString *str = @"type=focus-c"; NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding]; [request setHTTPBody:data]; //Step 3: Connect to the serverNSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
5. Proxy method for asynchronous requests
//Call this method when receiving the server response- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { NSHTTPURLResponse *res = (NSHTTPURLResponse *)response; NSLog(@"%@",[res allHeaderFields]); = [NSMutableData data]; } //Called when receiving the server's data transmission. This method is executed several times according to the data size.-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [ appendData:data]; } //Call this method after data is passed-(void)connectionDidFinishLoading:(NSURLConnection *)connection { NSString *receiveStr = [[NSString alloc]initWithData: encoding:NSUTF8StringEncoding]; NSLog(@"%@",receiveStr); } //When there is any error (network disconnection, connection timeout, etc.) during the network request process, this method will be entered-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { NSLog(@"%@",[error localizedDescription]);
The above is a comprehensive analysis of synchronous requests, asynchronous requests, GET requests, and POST requests in iOS introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!