自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

iPhone應(yīng)用用HTTP協(xié)議和服務(wù)器通信

移動(dòng)開發(fā) iOS
iPhone應(yīng)用用HTTP協(xié)議和服務(wù)器通信是本文要介紹的內(nèi)容,主要是來學(xué)習(xí)iphone應(yīng)用中的通信協(xié)議,具體內(nèi)容來看本文詳解。

iPhone應(yīng)用HTTP協(xié)議服務(wù)器通信是本文要介紹的內(nèi)容,主要是來學(xué)習(xí)iphone應(yīng)用中的通信協(xié)議,具體內(nèi)容來看本文詳解。

iPhone用http協(xié)議和服務(wù)器通信有兩種方式,一種是同步一種是異步的,所謂同步是指當(dāng)客戶端調(diào)用post/get的方式的函數(shù)向服務(wù)器發(fā)出數(shù)據(jù)請(qǐng)求后,該函數(shù)不會(huì)直接返回,只有得到服務(wù)器響應(yīng)或者請(qǐng)求時(shí)間timeout之后才會(huì)返回繼續(xù)執(zhí)行其它任務(wù)。異步采用回調(diào)的方式,即請(qǐng)求發(fā)送后,函數(shù)會(huì)立即返回,一旦服務(wù)器聯(lián)結(jié)成功操作系統(tǒng)會(huì)去觸發(fā)相應(yīng)的回調(diào)進(jìn)行相應(yīng)的處理。這和window的消息處理機(jī)制一樣。

同步一般用于一次性操作,如判斷當(dāng)前網(wǎng)絡(luò)是否可用等等。多的就不再一一介紹,在實(shí)現(xiàn)上面有兩點(diǎn)不同:

(1)在用NSURLConnect的時(shí)候一個(gè)調(diào)用同步函數(shù)一個(gè)調(diào)用了異步函數(shù)。

(2)異步的需要實(shí)現(xiàn)delegate的相關(guān)回調(diào)函數(shù)。

以下是參考代碼:

同步方式:

  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 

以下為相應(yīng)的回調(diào)函數(shù)

  1. // 收到響應(yīng)時(shí), 會(huì)觸發(fā)  
  2. - (void)connection:(NSURLConnection *)connection   didReceiveResponse:(NSURLResponse *)response  {  
  3. // 注意這里將NSURLResponse對(duì)象轉(zhuǎn)換成NSHTTPURLResponse對(duì)象才能去  
  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. //鏈接錯(cuò)誤    
  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. //接收數(shù)據(jù) 每收到一次數(shù)據(jù), 會(huì)調(diào)用一次  
  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. //接收結(jié)束  
  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.  

小結(jié):iPhone應(yīng)用HTTP協(xié)議服務(wù)器通信的內(nèi)容介紹完了,希望通過本文的學(xué)習(xí)能對(duì)你有所幫助!

責(zé)任編輯:zhaolei 來源: 互聯(lián)網(wǎng)
相關(guān)推薦

2021-06-16 07:34:32

Pythonsocket庫Python基礎(chǔ)

2019-08-01 15:25:17

Http服務(wù)器協(xié)議

2010-03-19 09:26:34

Java Socket

2010-08-26 10:01:50

DHCP服務(wù)器

2010-03-29 14:56:36

云計(jì)算

2018-08-23 09:16:22

2010-09-17 10:07:17

SIP協(xié)議SIP代理服務(wù)器

2023-04-26 07:36:44

緩存雪崩服務(wù)器架構(gòu)

2018-10-31 12:51:04

2009-02-12 14:12:00

2011-10-25 07:32:13

存儲(chǔ)服務(wù)器虛擬化

2018-12-20 08:50:53

TCPIP服務(wù)器

2009-02-17 18:36:59

存儲(chǔ)虛擬化服務(wù)器虛擬化虛擬化

2020-06-17 21:39:11

HTTP協(xié)議服務(wù)器

2009-02-12 15:51:00

squid代理服務(wù)器web服務(wù)器

2013-03-12 10:01:46

ARMPC服務(wù)器

2010-09-03 10:27:30

AMDARM

2016-01-28 10:04:10

虛擬化

2014-04-09 14:08:44

VDI存儲(chǔ)服務(wù)器技術(shù)

2014-07-14 15:52:08

VDI
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)