Fully resolve synchronous asynchronous GET POST requests in iOS

  • 2020-10-23 21:13:35
  • OfStack

First, I will introduce the meanings of synchronous request, asynchronous request, GET request and POST respectively in iOS, and then I will introduce them to you through examples one by one.

1. Once the synchronous request is sent, the program will stop the user interaction, and the next step can be performed until the server returns the data.

2. The asynchronous request will not block the main thread, but a new thread will be created to operate. After the user issues the asynchronous request, UI can still operate and the program can continue to run

3. GET requests that the parameters be written directly on the access path. Simple operation, but easy to be seen by the outside world, security is not high, the address of up to 255 bytes;

4, POST request, put the parameters in body. The POST request operation is relatively complex and requires separating the parameters from the address, but the security is high and the parameters are placed in body and are not easily captured.

1. Synchronize GET requests


// The first 1 Step, create URL
NSURL *url = [NSURL URLWithString:@"http://api.hudong.com/iphonexml.do?type=focus-c"];
// The first 2 Step through the URL Create a network request 
NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
//NSURLRequest Initialize method control 1 Parameters: request access path, and 2 Parameters: caching protocol, and 3 Network request timeout (seconds) 
 Where the cache protocol is an enumeration type containing: 
NSURLRequestUseProtocolCachePolicy (Basic Strategy) 
NSURLRequestReloadIgnoringLocalCacheData (Ignoring local cache) 
NSURLRequestReturnCacheDataElseLoad (Use the cache first, download from the original address if there is no local cache) 
NSURLRequestReturnCacheDataDontLoad (Use local cache, never download, if there is no local cache, the request fails, this policy is mostly used for offline operations) 
NSURLRequestReloadIgnoringLocalAndRemoteCacheData (Regardless of any caching policy, whether local or remote, always redownload from the original address) 
NSURLRequestReloadRevalidatingCacheData (If the local cache is valid, do not download it; otherwise, download it from the original address.) 
// The first 3 Step, connect to the server 
NSData *received = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *str = [[NSString alloc]initWithData:received encoding:NSUTF8StringEncoding];
NSLog(@"%@",str);

2. Synchronize POST requests


// The first 1 Step, create URL
NSURL *url = [NSURL URLWithString:@"http://api.hudong.com/iphonexml.do"];
// The first 2 Step, create the request 
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
[request setHTTPMethod:@"POST"];// Set the request mode to POST By default, GET
NSString *str = @"type=focus-c";// Set the parameters 
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:data];
// The first 3 Step, connect to the server 
NSData *received = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *str1 = [[NSString alloc]initWithData:received encoding:NSUTF8StringEncoding];
NSLog(@"%@",str1);

3, asynchronous GET request


// The first 1 Step, create url
NSURL *url = [NSURL URLWithString:@"http://api.hudong.com/iphonexml.do?type=focus-c"];
// The first 2 Step, create the request 
NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
// The first 3 Step, connect to the server 
NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];

4, Asynchronous POST request


// The first 1 Step, create url
NSURL *url = [NSURL URLWithString:@"http://api.hudong.com/iphonexml.do"];
// The first 2 Step, create the request 
NSMutableURLRequest *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];
// The first 3 Step, connect to the server 
NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];

5, asynchronous request proxy method


// This method is called when a server response is received 
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSHTTPURLResponse *res = (NSHTTPURLResponse *)response;
NSLog(@"%@",[res allHeaderFields]);
self.receiveData = [NSMutableData data];
}
// Called when data is received from the server and executed several times depending on the size of the data 
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.receiveData appendData:data];
}
// This method is called after the data is passed 
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *receiveStr = [[NSString alloc]initWithData:self.receiveData encoding:NSUTF8StringEncoding];
NSLog(@"%@",receiveStr);
}
// Any errors (disconnection, connection timeout, etc.) that occur during a network request enter this method 
-(void)connection:(NSURLConnection *)connection
didFailWithError:(NSError *)error
{
NSLog(@"%@",[error localizedDescription]);

Related articles: