处理同时进行多个网络请求的方法

[objc]  view plain copy print ?
  1. NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];  
  2.   
  3.     NSURLRequest *request = [NSURLRequest requestWithURL:url];  
  4.   
  5.     AFHTTPRequestOperation *operation1 = [[AFHTTPRequestOperation alloc] initWithRequest:request];  
  6.   
  7.     [operation1 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {  
  8.   
  9.         NSLog(@"Response: %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);  
  10.   
  11.     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {  
  12.   
  13.         NSLog(@"Error: %@", error);  
  14.   
  15.     }];  
  16.   
  17.       
  18.   
  19.       
  20.   
  21.     NSURL *url2 = [NSURL URLWithString:@"http://www.sohu.com"];  
  22.   
  23.     NSURLRequest *request2 = [NSURLRequest requestWithURL:url2];  
  24.   
  25.     AFHTTPRequestOperation *operation2 = [[AFHTTPRequestOperation alloc] initWithRequest:request2];  
  26.   
  27.     [operation2 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {  
  28.   
  29.         NSLog(@"Response2: %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);  
  30.   
  31.     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {  
  32.   
  33.         NSLog(@"Error: %@", error);  
  34.   
  35.     }];  
  36.   
  37.       
  38.   
  39.       
  40.   
  41.       
  42.   
  43.     NSURL *url3 = [NSURL URLWithString:@"http://www.sina.com"];  
  44.   
  45.     NSURLRequest *request3 = [NSURLRequest requestWithURL:url3];  
  46.   
  47.     AFHTTPRequestOperation *operation3 = [[AFHTTPRequestOperation alloc] initWithRequest:request3];  
  48.   
  49.     [operation3 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {  
  50.   
  51.         NSLog(@"Response3: %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);  
  52.   
  53.     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {  
  54.   
  55.         NSLog(@"Error: %@", error);  
  56.   
  57.     }];  
  58.   
  59.       
  60.   
  61.       
  62.   
  63.     //同时请求  
  64.   
  65.     NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];  
  66.   
  67.     [operationQueue setMaxConcurrentOperationCount:3];  
  68.   
  69.     [operationQueue addOperations:@[operation1, operation2, operation3] waitUntilFinished:NO];  
  70.   
  71.    
  72.   
  73.       
  74.   
  75.     //operation2 在 operation1 请求完成后执行  
  76.   
  77.     NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];  
  78.   
  79.     [operation2 addDependency:operation1];  
  80.   
  81.     [operationQueue addOperations:@[operation1, operation2, operation3] waitUntilFinished:NO];  

下边是POST请求:

[objc]  view plain copy print ?
  1. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://gowalla.com/friendships/request?user_id=1699"]];  
  2. [request setHTTPMethod:@"POST"];  
  3.   
  4. NSDictionary *headers = [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Token token=\"%@\"", kOAuthToken] forKey:@"Authorization"];  
  5. [request setAllHTTPHeaderFields:headers];  
  6.   
  7. AFHTTPRequestOperation *operation = [AFHTTPRequestOperation operationWithRequest:request completion:^(NSURLRequest *request, NSHTTPURLResponse *response, NSData *data, NSError *error) {  
  8.     BOOL HTTPStatusCodeIsAcceptable = [[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(200100)] containsIndex:[response statusCode]];  
  9.     if (HTTPStatusCodeIsAcceptable) {  
  10.         NSLog(@"Friend Request Sent");  
  11.     } else {  
  12.         NSLog(@"[Error]: (%@ %@) %@", [request HTTPMethod], [[request URL] relativePath], error);  
  13.     }  
  14. }];  
  15.   
  16. NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];  
  17. [queue addOperation:operation];  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值