【问题标题】:Send 100+ request continuously using NSURLConnection and wait for every response before sending next request使用 NSURLConnection 连续发送 100+ 个请求,并在发送下一个请求之前等待每个响应
【发布时间】:2015-07-15 03:53:58
【问题描述】:

如何使用 NSURLConnection 连续发送 100+ 个请求,并在发送下一个请求之前等待每个响应,在发送请求 100 次循环时,仅收到前四个或五个请求的响应,其余请求作为请求超时得到响应,请帮我提供一些示例代码或参考

NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
_Connection=[[NSURLConnection alloc]initWithRequest:request delegate:self startImmediately:YES];

【问题讨论】:

  • 为什么不在一个请求中发送它而不是单独发送 100 多个?
  • 您希望在发送下一个请求之前同步返回响应吗?这是你想要的吗?
  • 在您获得响应的委托方法中发送下一个请求。
  • 是的,你是绝对正确的 user2829759
  • 我使用for循环连续发送请求

标签: ios nsurlconnection nsurl nsurlrequest


【解决方案1】:

试试这个,

使用0 值作为参数调用startGenerator 方法,从您想要开始的位置开始。

+(void)startGenerator:(int)counter { 

     NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
    [NSURLConnection request
                                           queue:[NSOperationQueue currentQueue]
                               completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
           if (error == nil) {
              //Your Response parse and other stuff
              dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 5 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
                    // Make next request after  5 second after completion of current request.  
                    counter++;
                    if (counter<limitOfURL)
                         [self startGenerator];
              });
           }
   }];

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 2020-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多