IPhone applicationsUseHTTP protocolAndServerCommunication is the content to be introduced in this article, mainly to learnIphone applicationsFor more information about the communication protocol, see this article.
IPhone uses http protocol and server communication in two ways. One is synchronous and the other is asynchronous, synchronization means that when the client calls a post/get function to send a data request to the server, the function will not return directly, the server will continue to execute other tasks only after the server response or request time is timeout. Asynchronous callback is used, that is, after the request is sent, the function will return immediately. Once the server is connected successfully, the operating system will trigger the corresponding callback for corresponding processing. This is the same as the Message Processing Mechanism of window.
Synchronization is generally used for one-time operations, such as determining whether the current network is available. If there are too many, we will not introduce them one by one. There are two differences in implementation:
(1) When NSURLConnect is used, a synchronous function is called and an asynchronous function is called.
(2) asynchronous callback functions related to delegate must be implemented.
The following is the reference code:
Synchronization method:
- -(void)UpadaPost:(NSString *)strcontext URL:(NSString *)urlstr{
- NSLog(urlstr);
- NSLog(strcontext);
- assert(strcontext != NULL);
- assert(urlstr != NULL);
- NSData*postData=[strcontextdataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
- NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
- NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
- [request setURL:[NSURL URLWithString:urlstr]];
- [request setHTTPMethod:@"POST"]; [request setTimeoutInterval: 20];//setting timeout
- [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
- [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
- [request setHTTPBody:postData];
- NSURLResponse *respone;
- NSError *error;
- NSData*myReturn=[NSURLConnection sendSynchronousRequest:request returningResponse:&respone
- error:error];
- NSLog(@"%@", [[NSString alloc] initWithData:myReturn encoding:NSUTF8StringEncoding]);
- }
Asynchronous mode:
- -(void)UpadaPost:(NSString *)strcontext URL:(NSString *)urlstr{
- NSLog(urlstr);
- NSLog(strcontext);
- assert(strcontext != NULL);
- assert(urlstr != NULL);
- NSData *postData = [strcontext dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
- NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
- NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
- [request setURL:[NSURL URLWithString:urlstr]];
- [request setHTTPMethod:@"POST"]; [request setTimeoutInterval: 20];//setting timeout
- [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
- [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
- [request setHTTPBody:postData];
- NSURLConnection *conn=[[NSURLConnection alloc] initWithRequest:request delegate:self];
- if (conn)
- {
- NSLog(@"Connection success");
- [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
- [conn retain];
- }
- else
- {
- // inform the user that the download could not be made
- }
- }
- #pargma mark
The following are the corresponding callback functions.
- // Triggered when the response is received
- -(Void) connection :( NSURLConnection *) connection didReceiveResponse :( NSURLResponse *) response {
- // Note that only NSURLResponse objects can be converted to NSHTTPURLResponse objects.
- NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse *) response;
- If ([response respondsToSelector: @ selector (allHeaderFields)]) {
- NSDictionary * dictionary = [httpResponse allHeaderFields];
- NSLog ([dictionary description]);
- NSLog (@ "% d", [response statusCode]);
- }
- }
- // Link Error
- -(Void) connection :( NSURLConnection *) connection didFailWithError :( NSError *) error {
- // [Self initiate mselecw.mainthread: @ selector (httpConnectEnd) withObject: nil waitUntilDone: NO];
- NSLog (@ "% @", [error localizedDescription]);
- }
- // Called when a chunk of data has been downloaded.
- // Each time data is received, it is called.
- -(Void) connection :( NSURLConnection *) connection didReceiveData :( NSData *) data {
- // Process the downloaded chunk of data.
- NSLog (@ "% d", [data length]);
- // NSLog (@ "% @", [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]);
- // [Self initialize mselecw.mainthread: @ selector (updateProgress) withObject: nil waitUntilDone: NO];
- }
- // The receiving ends.
- -(Void) connectionDidFinishLoading :( NSURLConnection *) connection {
- NSLog (@ "% @", connection );
- // NSLog (@ "% lld", received _);
- // [Self initiate mselecw.mainthread: @ selector (httpConnectEnd) withObject: nil waitUntilDone: NO];
- // Set the condition which ends the run loop.
- }
Summary:IPhone applicationsUseHTTP protocolAndServerAfter the introduction of the communication content, I hope this article will help you!