移動網際網路時代,移動通信已經使手機用戶端必不可少的功能,我們的各種應用中也少不了網絡通信,需要增強手機用戶端和伺服器之間的交換。本文講介紹NSURLConnection中的同步請求,異步請求,get方法和post方法。
同步請求:資料會造成主線程阻塞,通常在大資料或者網絡不暢的情況下不使用,會使使用者與UI失去互動,出現程式的卡死,如果資料量少可以使用同步請求。
異步請求:異步請求不會阻塞主線程,會建立一個新的線程來做操作。
不管是異步請求還是同步請求,建立連接配接的步驟上雖然有所差别,但是不體上是一緻的:
1、建立NSURL
2、建立Request對象
3、建立NSURLConnection連接配接
下面通過一些簡單的例子做介紹:
1、#define URL @"http://www.baidu.com"
////////////////同步請求////////////////
//第一步:建立URL
NSURL *myURL=[NSURL URLWithString:URL];
//第二步:建立一個請求
NSURLRequest *myRequest=[NSURLRequest requestWithURL:myURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
//第三步:建立連接配接
NSURLResponse *myResponse=nil;
NSError *myErro=nil;
//向伺服器發起請求(發起之後,線程會一直等待伺服器響應,直到超出最大響應時間)
NSData *myData=[NSURLConnection sendSynchronousRequest:myRequest returningResponse:&myResponse error:&myErro];
NSLog(@"myData=%@",myData);//輸出資料
NSLog(@"myErro=%@",[myErro localizedDescription]);//輸出錯誤
2、異步請求///
//建立URL
NSURL *myURL=[NSURL URLWithString:URL];
//建立請求
NSURLRequest *myRuquest=[NSURLRequest requestWithURL:myURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
//建立連接配接(異步的response在專門的代理協定中實作)
[NSURLConnection connectionWithRequest:myRuquest delegate:self];
異步加載資料需要下面幾個方法常用的有四個方法:
#pragma mark URLConnectionDataDelegate
//接受伺服器響應--接收到伺服器回應的時候會執行該方法
-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
NSLog(@"伺服器響應");
self.myData = [NSMutableData dataWithCapacity:5000];
}
//接收伺服器資料--接收伺服器傳輸資料的時候會調用,會根據資料量的大小,多次執行
-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"伺服器傳回資料");
//将傳回資料放入緩存區
[self.myData appendData:data];
}
//顯示資料,直到所有的資料接收完畢
-(void) connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"資料接受完畢");
NSLog(@"myData=%@",self.myData);
}
//接受失敗的時候調用的方法(斷網或者是連接配接逾時)
-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"資料接受失敗,失敗原因:%@",[error localizedDescription]);
}
3、
///////////////get請求/////////////////////
NSString *myStr=self.TextFieldQQ.text;
NSString *strUrl=[@"http://webservice.webxml.com.cn/webservices/qqOnlineWebservice.asmx/qqCheckOnline?qqCode=" stringByAppendingFormat:@"%@",myStr];
//建立URL
NSURL *myUrl=[NSURL URLWithString:strUrl];
//建立請求
NSURLRequest *myRequest=[NSURLRequest requestWithURL:myUrl cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
//建立連接配接
[NSURLConnection connectionWithRequest:myRequest delegate:self];
4、
///post請求///////////////////////////////
NSString *postStr=@"http://webservice.webxml.com.cn/webservices/qqOnlineWebService.asmx/qqCheckOnline";
NSURL *postUrl=[NSURL URLWithString:postStr];
NSMutableURLRequest *postRequest=[NSMutableURLRequest requestWithURL:postUrl cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
//将參數做成字元串
NSString *postStr1=[NSString stringWithFormat: @"qqCode=%@",self.TextFieldQQ.text];
//轉換成NSData
NSData *postData=[postStr1 dataUsingEncoding:NSUTF8StringEncoding];
//差別點,将參數作為Body體
[postRequest setHTTPBody:postData];
//必須手動聲明目前的請求方式為POST(如果不設定,預設的是“GET”方法)
[postRequest setHTTPMethod:@"POST"];
//根據postRequest建立連接配接,以及執行的協定
[NSURLConnection connectionWithRequest:postRequest delegate:self];
GET請求:将參數直接寫在通路路徑上,如:@“http://webservice.webxml.com.cn/webservices/qqOnlineWebservice.asmx/qqCheckOnline?qqCode=”參數容易被外界看到,不安全
POST請求:将參數放在Body裡面,如:@"http://webservice.webxml.com.cn/webservices/qqOnlineWebService.asmx/qqCheckOnline"外界看不到參數,安全性比較高
注意:POST建立的NSURL是不帶參數的,需要将請求的參數放入Body中,是以需要用NSMutableURLRequest來建立請求。
參考文章:http://blog.sina.com.cn/s/blog_7ad8771f0101ce5a.html
http://m.blog.csdn.net/blog/xyz_lmn/8968182