Synchronous requests, asynchronous requests, GET requests, and POST requests for IOS
1. synchronous requests can request data from the Internet. Once a synchronous request is sent, the program stops user interaction until the server returns data,
2. asynchronous requests do not block the main thread, but create a new thread to operate. After the user sends an asynchronous request, the user can still operate the UI, and the program can continue to run.
3. For a GET request, write the parameters directly in the access path. The operation is simple, but it is easy to see by the outside world. The security is not high, and the address can be up to 255 bytes;
4. Put the parameters in the body in the POST request. POST request operations are relatively complex. Parameters and addresses must be separated. However, they are highly secure and placed in the body, which is not easy to capture.
1. synchronous GET request
// Step 1: Create a URL
NSURL * url = [NSURL URLWithString: @ "http://api.hudong.com/iphonexml.do? Type = focus-c "];
// Step 2: create a network request through URL
NSURLRequest * request = [[NSURLRequest alloc] initWithURL: url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 10];
// 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 is an enumeration type that includes:
NSURLRequestUseProtocolCachePolicy (Basic Policy)
NSURLRequestReloadIgnoringLocalCacheData (ignore local cache)
NSURLRequestReturnCacheDataElseLoad)
NSURLRequestReturnCacheDataDontLoad)
NSURLRequestReloadIgnoringLocalAndRemoteCacheData (regardless of any cache policy, whether local or remote, it is always downloaded from the original address)
NSURLRequestReloadRevalidatingCacheData (if the local cache is valid, it will not be downloaded. Otherwise, it will be downloaded again from the original address)
// 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 );
2. synchronous POST request
// Step 1: Create a URL
NSURL * url = [NSURL URLWithString: @ "http://api.hudong.com/iphonexml.do"];
// Step 2: Create a request
NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL: url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 10];
[Request setHTTPMethod: @ "POST"]; // sets the request Method to POST. The default value is GET.
NSString * str = @ "type = focus-c"; // set parameters
NSData * data = [str dataUsingEncoding: NSUTF8StringEncoding];
[Request setHTTPBody: data];
// Step 3: connect to the server
NSData * received = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];
NSString * str1 = [[NSString alloc] initWithData: encoded ed encoding: NSUTF8StringEncoding];
NSLog (@ "% @", str1 );
3. asynchronous GET request
// Step 1: Create a url
NSURL * url = [NSURL URLWithString: @ "http://api.hudong.com/iphonexml.do? Type = focus-c "];
// Step 2: Create a request
NSURLRequest * request = [[NSURLRequest alloc] initWithURL: url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 10];
// Step 3: connect to the server
NSURLConnection * connection = [[NSURLConnection alloc] initWithRequest: request delegate: self];
4. asynchronous POST request
// Step 1: Create a url
NSURL * url = [NSURL URLWithString: @ "http://api.hudong.com/iphonexml.do"];
// Step 2: Create a 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];
// Step 3: connect to the server
NSURLConnection * connection = [[NSURLConnection alloc] initWithRequest: request delegate: self];
5. Proxy Method for asynchronous requests
// Call this method when receiving a response from the server
-(Void) connection :( NSURLConnection *) connection didReceiveResponse :( NSURLResponse *) response
{
NSHTTPURLResponse * res = (NSHTTPURLResponse *) response;
NSLog (@ "% @", [res allHeaderFields]);
Self. receiveData = [NSMutableData data];
}
// Called when receiving data transmitted by the server. This method is executed several times based on the data size.
-(Void) connection :( NSURLConnection *) connection didReceiveData :( NSData *) data
{
[Self. receiveData appendData: data];
}
// Call this method after data transmission.
-(Void) connectionDidFinishLoading :( NSURLConnection *) connection
{
NSString * receiveStr = [[NSString alloc] initWithData: self. receiveData encoding: NSUTF8StringEncoding];
NSLog (@ "% @", receiveStr );
}
// Any errors (Network disconnection, connection timeout, etc.) occur during the network request.
-(Void) connection :( NSURLConnection *) connection
DidFailWithError :( NSError *) error
{
NSLog (@ "% @", [error localizedDescription]);