成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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基礎

2019-08-01 15:25:17

Http服務器協議

2010-03-19 09:26:34

Java Socket

2010-08-26 10:01:50

DHCP服務器

2010-03-29 14:56:36

云計算

2018-08-23 09:16:22

2018-10-31 12:51:04

2010-09-17 10:07:17

SIP協議SIP代理服務器

2023-04-26 07:36:44

緩存雪崩服務器架構

2009-02-12 14:12:00

2009-02-12 15:51:00

squid代理服務器web服務器

2010-09-03 10:27:30

AMDARM

2014-04-09 14:08:44

VDI存儲服務器技術

2014-07-14 15:52:08

VDI

2016-01-28 10:04:10

虛擬化

2013-03-12 10:01:46

ARMPC服務器

2020-06-17 21:39:11

HTTP協議服務器

2009-02-17 18:36:59

存儲虛擬化服務器虛擬化虛擬化

2011-10-25 07:32:13

存儲服務器虛擬化

2018-12-20 08:50:53

TCPIP服務器
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产农村一级国产农村 | 免费久久99精品国产婷婷六月 | 亚洲一区二区三区在线视频 | www.99热.com| 在线国产视频观看 | 国内自拍视频在线观看 | 国产人成在线观看 | 特黄级国产片 | 中文字幕丁香5月 | 精品1区| 韩日av片| 成人欧美一区二区三区黑人孕妇 | 欧美性a视频 | 欧美一区二区在线播放 | 日韩综合在线 | 欧美在线一区二区三区 | 麻豆精品久久 | 国产传媒视频在线观看 | www.欧美.com | 黄色免费av | 日韩精品人成在线播放 | 九九热在线免费视频 | 一级毛片在线看 | 黄色大片毛片 | 久久国产精品偷 | 日本精品一区二区三区视频 | 精品国产一区二区三区日日嗨 | 亚洲视频 欧美视频 | 亚洲高清在线 | 婷婷去俺也去 | 国产精品久久久久久久久久久久 | 欧美亚洲高清 | 久久精品中文字幕 | 中文字幕av在线一二三区 | 色就是色欧美 | 午夜精品久久久久久久久久久久 | 在线视频日韩精品 | 古典武侠第一页久久777 | 亚洲狠狠| 天天干在线播放 | 成人深夜小视频 |