iPhone應(yīng)用用HTTP協(xié)議和服務(wù)器通信
iPhone應(yīng)用用HTTP協(xié)議和服務(wù)器通信是本文要介紹的內(nèi)容,主要是來(lái)學(xué)習(xí)iphone應(yīng)用中的通信協(xié)議,具體內(nèi)容來(lá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ù)。
以下是參考代碼:
同步方式:
- -(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]);
 - }
 
異步方式:
- -(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
 
以下為相應(yīng)的回調(diào)函數(shù)
- // 收到響應(yīng)時(shí), 會(huì)觸發(fā)
 - - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
 - // 注意這里將NSURLResponse對(duì)象轉(zhuǎn)換成NSHTTPURLResponse對(duì)象才能去
 - NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
 - if ([response respondsToSelector:@selector(allHeaderFields)]) {
 - NSDictionary *dictionary = [httpResponse allHeaderFields];
 - NSLog([dictionary description]);
 - NSLog(@"%d",[response statusCode]);
 - }
 - }
 - //鏈接錯(cuò)誤
 - - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
 - //[self performSelectorOnMainThread:@selector(httpConnectEnd) withObject:nil waitUntilDone:NO];
 - NSLog(@"%@",[error localizedDescription]);
 - }
 - // Called when a chunk of data has been downloaded.
 - //接收數(shù)據(jù) 每收到一次數(shù)據(jù), 會(huì)調(diào)用一次
 - - (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 performSelectorOnMainThread:@selector(updateProgress) withObject:nil waitUntilDone:NO];
 - }
 - //接收結(jié)束
 - - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
 - NSLog(@"%@",connection);
 - //NSLog(@"%lld", received_);
 - //[self performSelectorOnMainThread:@selector(httpConnectEnd) withObject:nil waitUntilDone:NO];
 - // Set the condition which ends the run loop.
 - }
 
小結(jié):iPhone應(yīng)用用HTTP協(xié)議和服務(wù)器通信的內(nèi)容介紹完了,希望通過本文的學(xué)習(xí)能對(duì)你有所幫助!















 
 
 

 
 
 
 