如何使用NSURLConnection连续发送100+请求并在发送下一个请求之前等待每个响应,同时发送100个循环的请求,仅在前四或五个请求时收到响应,其余请求在请求超时时获得响应,请帮我提供一些示例代码或参考资料
NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
_Connection=[[NSURLConnection alloc]initWithRequest:request delegate:self startImmediately:YES];
答案 0 :(得分:1)
试试这个,
调用startGenerator
方法,将0
值作为您要开始的参数。
+(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];
});
}
}];
希望这对你有所帮助。