Implementation of ios synchronous Get requests and ios synchronous get requests
// Step 1: Create a URL
NSURL * url = [NSURL URLWithString: @ "http://itunes.apple.com/search? Term = & entity = software "];
// Step 2: create a network request through URL
NSURLRequest * request = [[NSURLRequest alloc] initWithURL: url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 10];
// Step 3: connect to the server
NSData * received = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];
NSString * str = [[NSString alloc] initWithData: encoded ed encoding: NSUTF8StringEncoding];
NSLog (@ "% @", str); // This is simple. It is done here, and str is the result of the request.
Note: The parameters in step 2 are described as follows:
// The first parameter of the NSURLRequest initialization method: Request access path, second parameter: Cache Protocol, and third parameter: Network request timeout (seconds)
// The cache Protocol contains the following enumerated types:
// NSURLRequestUseProtocolCachePolicy // (Basic Policy)
// NSURLRequestReloadIgnoringLocalCacheData // (ignore local cache)
// NSURLRequestReturnCacheDataElseLoad // (the cache is used first. If there is no local cache, It is downloaded from the original address)
// NSURLRequestReturnCacheDataDontLoad // (use local cache and never download. If no cache is available locally, the request fails. This policy is mostly used for offline operations) www.2cto.com
// NSURLRequestReloadIgnoringLocalAndRemoteCacheData // (regardless of any cache policy, whether it is local or remote, it is always downloaded from the original address)
// NSURLRequestReloadRevalidatingCacheData // (if the local cache is valid, it is not downloaded. In other cases, it is downloaded again from the original address)