兩種請求方式GET,POST
兩種請求方式的比較
相同點(diǎn):都能給服務(wù)器傳輸數(shù)據(jù)
不同點(diǎn):
1、給服務(wù)器傳輸數(shù)據(jù)的方式:
GET:通過網(wǎng)址字符串。POST:通過data
2、傳輸數(shù)據(jù)的大?。篏ET:?址字符串最多255字節(jié)。POST:使用NSData,容量超過1G
3、安全性:GET:所有傳輸給服務(wù)器的數(shù)據(jù),顯示在網(wǎng)址里,類似于密碼的明文輸入,直接可見。
POST:數(shù)據(jù)被轉(zhuǎn)成NSData(二進(jìn)制數(shù)據(jù)),類似于密碼的密文輸?入,?無法直接讀取。
連接方式
同步:使用一個(gè)線程(主線程)完成所有的工作,效率低,當(dāng)線程正在執(zhí)行一個(gè)任務(wù)的時(shí)候無法執(zhí)行另一個(gè)任務(wù),所有如果使用同步進(jìn)行網(wǎng)絡(luò)數(shù)據(jù)的請求,那么在該線程進(jìn)行網(wǎng)絡(luò)請求時(shí),暫時(shí)無法響應(yīng)用戶的點(diǎn)擊事件,用戶體驗(yàn)極差
異步:再開一個(gè)線程(子線程)去完成任務(wù),此時(shí),主線程依然可以監(jiān)聽用戶的點(diǎn)擊事件,不會造成卡頓,用戶體驗(yàn)較好
一般常用的就post 和 get 兩種比較常用,是.cn/WebServices/.asmx/getCountryCityByIp?theIpAddress=" :pStr];//轉(zhuǎn)換為URLNSURL *pURL = [NSURL URLWithString:strURL];//創(chuàng)建請求NSURLRequest *pRequest = [NSURLRequest requestWithURL:pURL cachePolicy: timeoutInterval:60];//向服務(wù)器發(fā)起請求[NSURLConnection connectionWithRequest:pRequest delegate:self];*///POST//與Get請求的第一個(gè)區(qū)別點(diǎn)(不帶參數(shù),參數(shù)附件在body體里)NSString *postStr = @".cn/WebServices/.asmx/getCountryCityByIp";//轉(zhuǎn)換為URLNSURL *postURL = [NSURL URLWithString:postStr];//第二個(gè)區(qū)別點(diǎn)(請求為NSMutableURLRequest)NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:postURL cachePolicy: timeoutInterval:60];//將參數(shù)做成一個(gè)字符串NSString *post1 = [NSString stringWithFormat:@"theIpAddress=%@",self.pTextIP.text];//轉(zhuǎn)換為NSDataNSData *postData = [post1 dataUsingEncoding:NSUTF8StringEncoding];//第三個(gè)區(qū)別點(diǎn)(將參數(shù)作為Body體)[postRequest setHTTPBody:postData];//第四點(diǎn)(必須手動聲明當(dāng)前的請求方式為POST)[postRequest setHTTPMethod:@"POST"];//向服務(wù)器發(fā)送請求[NSURLConnection connectionWithRequest:postRequest delegate:self];}3、委托中方法實(shí)現(xiàn)#pragma mark------URL delegate----- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{NSLog(@"服務(wù)器響應(yīng)");_pMutableStr = [[NSMutableString alloc]init];}- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{NSLog(@"接收數(shù)據(jù)");NSString *pStr = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];[_pMutableStr appendString:pStr];NSLog(@"pStr = %@",_pMutableStr);}- (void):(NSURLConnection *)connection{NSLog(@"接收數(shù)據(jù)成功");NSMutableString *pValue = [[NSMutableString alloc]init];//獲取字符串中的漢字部分for(int i = 0; i 0x4e00 && a < 0x9fff){[pValue appendString:[_pMutableStr substringWithRange:NSMakeRange(i, 1)]];}}NSLog(@"pValue = %@",pValue);self.pValueLabel.text = pValue;[pValue release];}- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{NSLog(@"Error = %@",[error localizedDescription]);}//收回鍵盤的方法- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{[self.view endEditing:YES];}4、到目前為止,功。
聲明:本網(wǎng)站尊重并保護(hù)知識產(chǎn)權(quán),根據(jù)《信息網(wǎng)絡(luò)傳播權(quán)保護(hù)條例》,如果我們轉(zhuǎn)載的作品侵犯了您的權(quán)利,請?jiān)谝粋€(gè)月內(nèi)通知我們,我們會及時(shí)刪除。
蜀ICP備2020033479號-4 Copyright ? 2016 學(xué)習(xí)鳥. 頁面生成時(shí)間:2.565秒