When we load the network, there are two different ways to load the network. One is POST and the other is get.
1. Let's first look at the internal composition instances of the two request methods.
Get request:
GET /webservices/qqOnlineWebService.asmx/qqCheckOnline?qqCode=string HTTP/1.1Host: webservice.webxml.com.cnHTTP/1.1 200 OKContent-Type: text/xml; charset=utf-8Content-Length: length
string
Post request:
POST /webservices/qqOnlineWebService.asmx/qqCheckOnline HTTP/1.1Host: webservice.webxml.com.cnContent-Type: application/x-www-form-urlencodedContent-Length: lengthqqCode=stringHTTP/1.1 200 OKContent-Type: text/xml; charset=utf-8Content-Length: length
string
2. post and get can be used for Synchronous requests and asynchronous requests respectively. Here we will only introduce asynchronous requests
1) get asynchronous request
NSString * qqStr = self.qqTextFlied.text; NSString *str = [NSString stringWithFormat:@"http://webservice.webxml.com.cn/webservices/qqOnlineWebService.asmx/qqCheckOnline?qqCode=%@",qqStr ]; NSURL *url = [NSURL URLWithString:str]; NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10]; [NSURLConnection connectionWithRequest:request delegate:self];
Proxy methods are not described in detail.
2) post asynchronous request:
// Post request // 1. The url does not have the NSString * string = @ "http://webservice.webxml.com.cn/webservices/qqOnlineWebService.asmx/qqCheckOnline"; NSURL * postURL = [NSURL URLWithString: string]; // 2. Create a request NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL: postURL cachePolicy: Required timeoutInterval: 10]; [request setHTTPMethod: @ "POST"]; NSString * param = [NSString stringWithFormat: @ "qqCode = % @", self. qqTextFlied. text]; NSData * postData = [param dataUsingEncoding: NSUTF8StringEncoding]; [request setHTTPBody: postData]; [NSURLConnection connectionWithRequest: request delegate: self];
Similarities and differences between post requests and get requests:
URL format: the post request is a URL without parameters, while the Get request contains parameters (which need to be spliced ). For Post requests, you need to set the method setHTTPMethod: @ "POST", and the default status is @ "GET" POST request. You need to set the request body (NSdata ), the transmitted data is encapsulated by parameters. The following describes the specific code:
// Post request // 1. The url does not have the NSString * string = @ "http://webservice.webxml.com.cn/webservices/qqOnlineWebService.asmx/qqCheckOnline"; NSURL * postURL = [NSURL URLWithString: string]; // 2. Create a request NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL: postURL cachePolicy: Required timeoutInterval: 10]; [request setHTTPMethod: @ "POST"]; NSString * param = [NSString stringWithFormat: @ "qqCode = % @", self. qqTextFlied. text]; NSData * postData = [param dataUsingEncoding: NSUTF8StringEncoding]; [request setHTTPBody: postData]; [NSURLConnection connectionWithRequest: request delegate: self];
An article on the Internet about get, post, and asynchronous synchronization is better. URL: http://www.open-open.com/lib/view/open1355055986679.html
Advantages and disadvantages: 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. Synchronize GET requests // step 1, create url nsurl * url = [NSURL URLWithString: @ "http://api.hudong.com/iphonexml.do? Type = focus-c "]; // Step 2: create a network request via URL NSURLRequest * request = [[NSURLRequest alloc] initWithURL: url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 10]; // The first parameter of the NSURLRequest initialization method is the request access path, the second parameter is the cache Protocol, and the third parameter is the network request timeout (in seconds). The cache Protocol is an enumeration type that includes: NSURLRequestUseProtocolCachePolicy (Basic Policy) NSURLRequestReloadIgnoringLocalCacheData (ignore local cache) Ignore (use cache first, if there is no local cache, download from the original address) NSURLReque StReturnCacheDataDontLoad (use local cache, Never download, if there is no local cache, the request fails, this policy is mostly used for offline operations) NSURLRequestReloadIgnoringLocalAndRemoteCacheData (ignore any cache policy, whether local or remote, always re-download from the original address) NSURLRequestReloadRevalidatingCacheData (if the local cache is valid, it will not be downloaded and will be re-downloaded from the original address in any case) // step 3, connect to the server NSData * received = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil]; NSString * str = [[NSString alloc] initWithData: received encoding: NSU TF8StringEncoding]; NSLog (@ "% @", str); 2. Synchronize the POST request // step 1, create url nsurl * url = [NSURL URLWithString: @ "http://api.hudong.com/iphonexml.do"]; // Step 2: Create a request NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL: url cachePolicy: timeoutInterval: 10]; [request setHTTPMethod: @ "POST"]; // set the request method to post. The default value is GET NSString * str = @ "type = focus-c"; // set the parameter 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 url NSURL * url = [NSURL URLWithString: @ "http://api.hudong.com/iphonexml.do? Type = focus-c "]; // Step 2: Create a request for 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. Create a url NSURL * url = [NSURL URLWithString: @ "http://api.hudong.com/iphonexml.do"]; // step 2, create the request NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL: url cachePolicy: 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. The proxy method of the asynchronous request // call this method when the server receives a response-(void) connection :( NSURLConnection *) connection didReceiveResponse :( NSURLResponse *) response {NSHTTPURLResponse * res = (NSHTTPURLResponse *) response; NSLog (@ "% @", [res allHeaderFields]); self. receiveData = [NSMutableData data];} // this method is called when the server receives the data transmitted. 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.) during network request) will enter this method-(void) connection :( NSURLConnection *) connection didFailWithError :( NSError *) error {NSLog (@ "% @", [error localizedDescription]);