Objective-c synchronous request, asynchronous request, get request, POST request

Source: Internet
Author: User



This post consists of: http://www.sollyu.com/objective-c-synchronization-requests-requests-for-asynchronous-requests-get-and-post-requests/


Description


1 The synchronization request can request data from the Internet, once the synchronization request is sent, the program will stop the user interaction until the server returns the data to complete before the next operation.



2 asynchronous requests do not block the main thread, but create a new thread to operate, and after the user makes an asynchronous request, the UI can still be manipulated and the program can continue to run



3 Get request to write parameters directly on the access path. Simple operation, but easy to be seen by the outside, security is not high, address up to 255 bytes;



4 POST request, put the parameters inside the body. Post request operation is relatively complex, need to separate parameters and addresses, but high security, parameters placed inside the body, not easily captured.


Synchronizing GET Requests
 
// First step, create URL
NSURL * url = [NSURL URLWithString: @ "http://www.shiniv.com/test.php?type=get"];

// The second step is to create a network request through the URL
NSURLRequest * 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)
/ * Where the cache protocol is an enumerated type containing:
NSURLRequestUseProtocolCachePolicy (base policy)
NSURLRequestReloadIgnoringLocalCacheData (ignore local cache)
NSURLRequestReturnCacheDataElseLoad (use the 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, this strategy is mostly used for offline operations)
NSURLRequestReloadIgnoringLocalAndRemoteCacheData (regardless of any caching strategy, whether local or remote, always re-download from the original address)
NSURLRequestReloadRevalidatingCacheData (Do not download if the local cache is valid, and re-download from the original address in any other case) * /
// The third step is to connect to the server
NSData * received = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];

NSString * str = [[NSString alloc] initWithData: received encoding: NSUTF8StringEncoding];
NSLog (@ "% @", str);
Synchronizing a POST request
// First step, create URL
NSURL * url = [NSURL URLWithString: @ "http://www.shiniv.com/test.php?type=get"];

// The second step is to create a network request through the URL
NSURLRequest * 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)
/ * Where the cache protocol is an enumerated type containing:
NSURLRequestUseProtocolCachePolicy (base policy)
NSURLRequestReloadIgnoringLocalCacheData (ignore local cache)
NSURLRequestReturnCacheDataElseLoad (use the 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, this strategy is mostly used for offline operations)
NSURLRequestReloadIgnoringLocalAndRemoteCacheData (regardless of any caching strategy, whether local or remote, always re-download from the original address)
NSURLRequestReloadRevalidatingCacheData (Do not download if the local cache is valid, and re-download from the original address in any other case) * /
// The third step is to connect to the server
NSData * received = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];

NSString * str = [[NSString alloc] initWithData: received encoding: NSUTF8StringEncoding];
NSLog (@ "% @", str); 
Asynchronous GET request
// First step, create url
NSURL * url = [NSURL URLWithString: @ "http://www.shiniv.com/test.php?type=get"];
// The second step is to create a request
NSURLRequest * request = [[NSURLRequest alloc] initWithURL: url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 10];
// The third step is to connect to the server
NSURLConnection * connection = [[NSURLConnection alloc] initWithRequest: request delegate: self]; 
Asynchronous POST request
// First step, create url
NSURL * url = [NSURL URLWithString: @ "http://www.shiniv.com/test.php"];
// The second step is to 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];
// The third step is to connect to the server
NSURLConnection * connection = [[NSURLConnection alloc] initWithRequest: request delegate: self]; 
Proxy methods for asynchronous requests
// Call this method 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 the server transmits data, this method is executed several times according to the data size
-(void) connection: (NSURLConnection *) connection didReceiveData: (NSData *) data
{
     [self.receiveData appendData: data];
}
// Call this method after the data is transferred
-(void) connectionDidFinishLoading: (NSURLConnection *) connection
{
     NSString * receiveStr = [[NSString alloc] initWithData: self.receiveData encoding: NSUTF8StringEncoding];
     NSLog (@ "% @", receiveStr);
}
// In the process of network request, any error (network disconnection, connection timeout, etc.) will enter this method
-(void) connection: (NSURLConnection *) connection
  didFailWithError: (NSError *) error
{
     NSLog (@ "% @", [error localizedDescription]);
} 


Objective-c synchronous request, asynchronous request, get request, POST request


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.