iPhone应用用HTTP协议和服务器通信

移动开发 iOS
iPhone应用用HTTP协议和服务器通信是本文要介绍的内容,主要是来学习iphone应用中的通信协议,具体内容来看本文详解。

iPhone应用HTTP协议服务器通信是本文要介绍的内容,主要是来学习iphone应用中的通信协议,具体内容来看本文详解。

iPhone用http协议和服务器通信有两种方式,一种是同步一种是异步的,所谓同步是指当客户端调用post/get的方式的函数向服务器发出数据请求后,该函数不会直接返回,只有得到服务器响应或者请求时间timeout之后才会返回继续执行其它任务。异步采用回调的方式,即请求发送后,函数会立即返回,一旦服务器联结成功操作系统会去触发相应的回调进行相应的处理。这和window的消息处理机制一样。

同步一般用于一次性操作,如判断当前网络是否可用等等。多的就不再一一介绍,在实现上面有两点不同:

(1)在用NSURLConnect的时候一个调用同步函数一个调用了异步函数。

(2)异步的需要实现delegate的相关回调函数。

以下是参考代码:

同步方式:

  1. -(void)UpadaPost:(NSString *)strcontext URL:(NSString *)urlstr{  
  2. NSLog(urlstr);  
  3. NSLog(strcontext);  
  4. assert(strcontext != NULL);  
  5. assert(urlstr != NULL);  
  6. NSData*postData=[strcontextdataUsingEncoding:NSASCIIStringEncoding  allowLossyConversion:YES];   
  7. NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];   
  8. NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];   
  9. [request setURL:[NSURL URLWithString:urlstr]];   
  10. [request setHTTPMethod:@"POST"]; [request setTimeoutInterval: 20];//setting timeout  
  11. [request setValue:postLength forHTTPHeaderField:@"Content-Length"];   
  12. [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];   
  13. [request setHTTPBody:postData];   
  14. NSURLResponse *respone;  
  15. NSError *error;  
  16. NSData*myReturn=[NSURLConnection  sendSynchronousRequest:request returningResponse:&respone  
  17. error:error];  
  18. NSLog(@"%@", [[NSString alloc] initWithData:myReturn encoding:NSUTF8StringEncoding]);  

异步方式:

  1. -(void)UpadaPost:(NSString *)strcontext URL:(NSString *)urlstr{  
  2. NSLog(urlstr);  
  3. NSLog(strcontext);  
  4. assert(strcontext != NULL);  
  5. assert(urlstr != NULL);  
  6. NSData *postData = [strcontext dataUsingEncoding:NSASCIIStringEncoding  allowLossyConversion:YES];   
  7. NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];   
  8. NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];   
  9. [request setURL:[NSURL URLWithString:urlstr]];   
  10. [request setHTTPMethod:@"POST"]; [request setTimeoutInterval: 20];//setting timeout  
  11. [request setValue:postLength forHTTPHeaderField:@"Content-Length"];   
  12. [request setValue:@"application/x-www-form-urlencoded"  forHTTPHeaderField:@"Content-Type"];   
  13. [request setHTTPBody:postData];   
  14. NSURLConnection *conn=[[NSURLConnection alloc] initWithRequest:request  delegate:self];   
  15. if (conn)     
  16. {   
  17. NSLog(@"Connection success");  
  18. [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;  
  19. [conn retain];  
  20. }     
  21. else     
  22. {   
  23. // inform the user that the download could not be made   
  24. }   
  25. }  
  26. #pargma mark 

以下为相应的回调函数

  1. // 收到响应时, 会触发  
  2. - (void)connection:(NSURLConnection *)connection   didReceiveResponse:(NSURLResponse *)response  {  
  3. // 注意这里将NSURLResponse对象转换成NSHTTPURLResponse对象才能去  
  4. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;  
  5. if ([response respondsToSelector:@selector(allHeaderFields)]) {  
  6. NSDictionary *dictionary = [httpResponse allHeaderFields];  
  7. NSLog([dictionary description]);  
  8. NSLog(@"%d",[response statusCode]);  
  9. }  
  10. }  
  11. //链接错误    
  12. - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {  
  13. //[self performSelectorOnMainThread:@selector(httpConnectEnd) withObject:nil  waitUntilDone:NO];  
  14. NSLog(@"%@",[error localizedDescription]);  
  15. }  
  16. // Called when a chunk of data has been downloaded.  
  17. //接收数据 每收到一次数据, 会调用一次  
  18. - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {  
  19. // Process the downloaded chunk of data.  
  20. NSLog(@"%d", [data length]);  
  21. //NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);  
  22. //[self performSelectorOnMainThread:@selector(updateProgress) withObject:nil  waitUntilDone:NO];  
  23. }  
  24. //接收结束  
  25. - (void)connectionDidFinishLoading:(NSURLConnection *)connection {  
  26. NSLog(@"%@",connection);  
  27. //NSLog(@"%lld", received_);  
  28. //[self performSelectorOnMainThread:@selector(httpConnectEnd) withObject:nil  waitUntilDone:NO];  
  29. // Set the condition which ends the run loop.  

小结:iPhone应用HTTP协议服务器通信的内容介绍完了,希望通过本文的学习能对你有所帮助!

责任编辑:zhaolei 来源: 互联网
相关推荐

2021-06-16 07:34:32

Pythonsocket库Python基础

2010-03-19 09:26:34

Java Socket

2019-08-01 15:25:17

Http服务器协议

2010-08-26 10:01:50

DHCP服务器

2010-03-29 14:56:36

云计算

2018-08-23 09:16:22

2023-04-26 07:36:44

缓存雪崩服务器架构

2010-09-17 10:07:17

SIP协议SIP代理服务器

2018-10-31 12:51:04

2009-02-12 14:12:00

2011-10-25 07:32:13

存储服务器虚拟化

2009-02-17 18:36:59

存储虚拟化服务器虚拟化虚拟化

2018-12-20 08:50:53

TCPIP服务器

2009-02-12 15:51:00

squid代理服务器web服务器

2013-03-12 10:01:46

ARMPC服务器

2016-01-28 10:04:10

虚拟化

2014-07-14 15:52:08

VDI

2014-04-09 14:08:44

VDI存储服务器技术

2010-09-03 10:27:30

AMDARM

2009-12-22 16:55:58

惠普服务器
点赞
收藏

51CTO技术栈公众号