Objective-C 之同步請求、異步請求、GET請求、POST請求

本篇文章由:http://www.sollyu.com/objective-c-synchronization-requests-requests-for-asynchronous-requests-get-and-post-requests/php

說明

1 同步請求能夠從因特網請求數據,一旦發送同步請求,程序將中止用戶交互,直至服務器返回數據完成,才能夠進行下一步操做,objective-c

2 異步請求不會阻塞主線程,而會創建一個新的線程來操做,用戶發出異步請求後,依然能夠對UI進行操做,程序能夠繼續運行緩存

3 GET請求,將參數直接寫在訪問路徑上。操做簡單,不過容易被外界看到,安全性不高,地址最多255字節;安全

4 POST請求,將參數放到body裏面。POST請求操做相對複雜,須要將參數和地址分開,不過安全性高,參數放在body裏面,不易被捕獲。服務器

同步GET請求

//第一步,建立URL
NSURL *url = [NSURL URLWithString:@"http://www.shiniv.com/test.php?type=get"];

//第二步,經過URL建立網絡請求
NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
//NSURLRequest初始化方法第一個參數:請求訪問路徑,第二個參數:緩存協議,第三個參數:網絡請求超時時間(秒)
/*其中緩存協議是個枚舉類型包含:
NSURLRequestUseProtocolCachePolicy(基礎策略)
NSURLRequestReloadIgnoringLocalCacheData(忽略本地緩存)
NSURLRequestReturnCacheDataElseLoad(首先使用緩存,若是沒有本地緩存,才從原地址下載)
NSURLRequestReturnCacheDataDontLoad(使用本地緩存,從不下載,若是本地沒有緩存,則請求失敗,此策略多用於離線操做)
NSURLRequestReloadIgnoringLocalAndRemoteCacheData(無視任何緩存策略,不管是本地的仍是遠程的,老是從原地址從新下載)
NSURLRequestReloadRevalidatingCacheData(若是本地緩存是有效的則不下載,其餘任何狀況都從原地址從新下載)*/
//第三步,鏈接服務器
NSData *received = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

NSString *str = [[NSString alloc]initWithData:received encoding:NSUTF8StringEncoding];
NSLog(@"%@",str);

同步POST請求

//第一步,建立URL
NSURL *url = [NSURL URLWithString:@"http://www.shiniv.com/test.php"];
//第二步,建立請求
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
[request setHTTPMethod:@"POST"];//設置請求方式爲POST,默認爲GET
NSString *str = @"type=focus-c";//設置參數
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:data];
//第三步,鏈接服務器
NSData *received = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *str1 = [[NSString alloc]initWithData:received encoding:NSUTF8StringEncoding];
NSLog(@"%@",str1);

異步GET請求

//第一步,建立url
NSURL *url = [NSURL URLWithString:@"http://www.shiniv.com/test.php?type=get"];
//第二步,建立請求
NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
//第三步,鏈接服務器
NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];

異步POST請求

//第一步,建立url
NSURL *url = [NSURL URLWithString:@"http://www.shiniv.com/test.php"];
//第二步,建立請求
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
[request setHTTPMethod:@"POST"];
NSString *str = @"type=focus-c";
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:data];
//第三步,鏈接服務器
NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];

異步請求的代理方法

//接收到服務器迴應的時候調用此方法
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSHTTPURLResponse *res = (NSHTTPURLResponse *)response;
    NSLog(@"%@",[res allHeaderFields]);
    self.receiveData = [NSMutableData data];

}
//接收到服務器傳輸數據的時候調用,此方法根據數據大小執行若干次
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.receiveData appendData:data];
}
//數據傳完以後調用此方法
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSString *receiveStr = [[NSString alloc]initWithData:self.receiveData encoding:NSUTF8StringEncoding];
    NSLog(@"%@",receiveStr);
}
//網絡請求過程當中,出現任何錯誤(斷網,鏈接超時等)會進入此方法
-(void)connection:(NSURLConnection *)connection
 didFailWithError:(NSError *)error
{
    NSLog(@"%@",[error localizedDescription]);
}
相關文章
相關標籤/搜索