1. 程式人生 > >iOS原生網路請求

iOS原生網路請求

1、     同步GET請求
 
    //第一步,建立URL
 
    NSURL *url = [NSURL URLWithString:@"http://api.hudong.com/iphonexml.do?type=focus-c"];
 
     
 
    //第二步,通過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);
 
2、同步POST請求
 
    //第一步,建立URL
 
    NSURL *url = [NSURL URLWithString:@"http://api.hudong.com/iphonexml.do"];
 
    //第二步,建立請求
 
    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);
3、非同步GET請求
 
    //第一步,建立url
 
    NSURL *url = [NSURL URLWithString:@"http://api.hudong.com/iphonexml.do?type=focus-c"];
 
    //第二步,建立請求
 
    NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
 
    //第三步,連線伺服器
 
    NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
4、非同步POST請求
 
    //第一步,建立url
 
    NSURL *url = [NSURL URLWithString:@"http://api.hudong.com/iphonexml.do"];
 
    //第二步,建立請求
 
    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];
 
5、非同步請求的代理方法
 
//接收到伺服器迴應的時候呼叫此方法
 
- (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]);

}