值在NSURL请求异步中混淆

时间:2014-01-05 01:46:47

标签: ios iphone objective-c nsurlconnection nsurlrequest

我异步进行NSURL请求。它在大多数情况下工作正常,但有时它会为主机返回200 OK,这显然是假的。这是连接的代码:

-(void)checkConnectionForHost:(NSString*)host completion:(completion_t)completionHandler
{
   NSURLRequest* request = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:host]];
   if([NSURLConnection canHandleRequest:request]){
      [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc]init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
         if(completionHandler)
         {
            completionHandler(connectionError == nil && [(NSHTTPURLResponse*)response statusCode]==200);
         }
      }];
   }

}

完成处理程序:

typedef void (^completion_t)(BOOL isReachable);

我在collectionView:cellForItem...委托方法中为我的数据源中的每个单元调用此命令。

我知道这不起作用的原因是因为我发送了一些随机主机,例如8107341897309,它立即返回0状态,但随后更改为200。此外,如果我使用字符串发送值,它将保持0。当我发送一个只有整数或小数的值时,这会不断跳跃。任何见解都会很棒!

编辑:因此,有时连接时会返回0,有时会返回200。我想说的是结果不一致。

编辑2:决定记录我在collectionView:cellForItemAtIndexPath得到的结果,并得到了这个:

    2014-01-04 19:22:53.390 ServerObserver[5624:4703] http://192.168.1.11 is Reachable? : 1. Index: 0
2014-01-04 19:22:53.414 ServerObserver[5624:3707] http://apple.com is Reachable? : 1. Index: 6
2014-01-04 19:22:53.619 ServerObserver[5624:4003] http://192.168.1.11 is Reachable? : 1. Index: 0
2014-01-04 19:22:53.697 ServerObserver[5624:5603] http://google.com is Reachable? : 1. Index: 1
2014-01-04 19:22:53.696 ServerObserver[5624:4003] http://pandora.com is Reachable? : 1. Index: 5
2014-01-04 19:22:53.705 ServerObserver[5624:4003] http://apple.com is Reachable? : 1. Index: 6
2014-01-04 19:22:53.732 ServerObserver[5624:5603] http://reddit.com is Reachable? : 1. Index: 4
2014-01-04 19:22:53.781 ServerObserver[5624:1803] http://google.com is Reachable? : 1. Index: 1
2014-01-04 19:22:53.893 ServerObserver[5624:5603] http://twitter.com is Reachable? : 1. Index: 3
2014-01-04 19:22:53.896 ServerObserver[5624:5603] http://reddit.com is Reachable? : 1. Index: 4
2014-01-04 19:22:53.891 ServerObserver[5624:1803] http://pandora.com is Reachable? : 1. Index: 5
2014-01-04 19:22:54.038 ServerObserver[5624:4003] http://twitter.com is Reachable? : 1. Index: 3
2014-01-04 19:22:54.268 ServerObserver[5624:5603] http://facebook.com is Reachable? : 1. Index: 2
2014-01-04 19:22:54.317 ServerObserver[5624:5603] http://facebook.com is Reachable? : 1. Index: 2

有些事情需要注意的是,垃圾IP甚至不会出现在这里,因为我检查了以前的方法,如果可以做出请求""。但是,为什么每个IP被调用两次?每个单元只有一个IP。

1 个答案:

答案 0 :(得分:0)

为什么不使用检查url是否存在如下:

- (BOOL)urlExistsOnServer:(NSString *)strURL
{
    NSURL *url = [NSURL URLWithString:strURL];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"HEAD"];
    NSURLResponse *response = nil;
    NSError *error=nil;
    NSData *data = [[NSData alloc] initWithData:[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]];
    NSMutableArray *json = (NSMutableArray*)[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    NSString *retVal = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    // you can use retVal , ignore if you don't need.
    NSInteger httpStatus = [((NSHTTPURLResponse *)response) statusCode];
    NSLog(@"responsecode:%d retVal:%@\njson:%@", httpStatus,retVal,json);

    if(httpStatus != 200)
    {
        return NO;
    }
    return YES;
}

如果您需要异步检查URL,则需要优化此代码,但这对我有用。