Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios NSURL请求异步中的值混淆_Ios_Iphone_Objective C_Nsurlconnection_Nsurlrequest - Fatal编程技术网

Ios NSURL请求异步中的值混淆

Ios NSURL请求异步中的值混淆,ios,iphone,objective-c,nsurlconnection,nsurlrequest,Ios,Iphone,Objective C,Nsurlconnection,Nsurlrequest,我正在异步执行一个NSURL请求。它在大多数情况下工作正常,但有时它会返回一个200ok,这显然是假的主机。以下是连接的代码: -(void)checkConnectionForHost:(NSString*)host completion:(completion_t)completionHandler { NSURLRequest* request = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:host]]; if(

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

-(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…
delegate方法中为数据源中的每个单元格调用此命令

我知道这不起作用的原因是因为我发送了一些随机主机,如
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。

为什么不使用检查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,则需要改进此代码,但这对我来说很有效。

是否需要解释:“立即返回0状态,但随后更改为200”?请使用Charles Proxy之类的网络分析器查看发送和接收的确切内容。对于混淆,我深表歉意,我已经更新了相反的相关:当它返回零时,什么是
响应(我打赌它是
nil
),什么是
connectionError
(我打赌它报告了连接错误)?当它是
200
时,
数据是什么(我敢打赌,如果你看它,把它转换成字符串,它可能是由你的ISP生成的HTML页面,上面写着“哦,我找不到那个网站,你是说X吗?”)我的ISP(Verizon FiOS)有这个令人难以置信的恼人功能(包括行为的不一致性).使用此代码与OPs代码相比没有任何优势,除非头部可能更好。但是,这种方法无法可靠地工作:例如,如果服务器需要身份验证,它可能会使用不同于200(OK)的状态代码进行响应,但实际上它是“可访问的”。(请参阅SCNetworkReachability以测试可达性)谢谢,在我的应用程序中,服务器是已知的,我通过在URL中添加文件名来检查文件是否存在。所以在我的例子中,这段代码非常有效(;