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