检查有效的IP与NSURLConnection的连接(Checking for valid IP f

2019-09-23 01:29发布

目前,我有试图打开一个基于某些服务器上我与通信网页视图的AP。

不过,我允许用户在万一输入自己的服务器IP地址都在用iPhone / iPad和服务器(或其它设备)的能力是不一样的网络上。 不过,我尝试使用NSURLConnection的检测,如果我能打开与给定的IP然而NSURLConnection的永远不会返回一个错误的连接,即使服务器地址(甚至是随机的网址)完全是伪造的。

在.H

        @interface DetailViewController : UIViewController <UIPopoverControllerDelegate, UISplitViewControllerDelegate, UITableViewDataSource, UITableViewDelegate,UIWebViewDelegate, NSURLConnectionDataDelegate> {

在.M相关代码

   - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:
         (NSIndexPath *)indexPath
       {
        dev_ip = @"http://www.asdfasfdfa.com/";
        //dev_ip = (random ip)
        NSMutableURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:dev_ip]];

        NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
        if (conn) {
            NSLog(@"Connection established");    

        }
        else{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:[NSString stringWithFormat:@"No Device at designated IP"]

                                                           delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
            [alert show];
        }
      }

这个if / else语句总是输出“已建立连接。” 这是不是NSURLConnection的不应该被用来做什么? 如果是的话,我可以用,以检测在连接指定IP的设备。 我需要从尝试连接到恶意IP的有啥这样做的最佳方法停止用户?

Answer 1:

NSURLConnection的,与代表团一起使用时,会调用委托方法,当它连接,连接失败和接收数据。 你应该看看NSURLConnectionDelegate。

这里有一个简单的例子:

    // In your .h

    @interface MyClass : NSObject <NSURLConnectionDelegate, NSURLConnectionDataDelegate>

    @end

编辑你真正需要双方的代表。


    // In your .m

    @implementation MyClass

    - (void)myMethodToConnect {

         NSString *dev_ip = @"http://74.125.137.101"; // Google.com

         NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:dev_ip]];

         NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    }

    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

        switch ([(NSHTTPURLResponse *)response statusCode]) { // Edited this!
            case 200: {
                NSLog(@"Received connection response!");
                break;
            }
            default: {
                NSLog(@"Something bad happened!");
                break;
            }
        }

    }

    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
        NSLog(@"Error connecting. Error: %@", error);
    }
    @end

此外,刚刚把它扔在那里过,你不一定必须使用异步调用。 你可以把它不需要你实现一个委托同步调用。 这是如何做:

    NSString *dev_ip = @"http://www.asdfasdfasdf.com/";
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:dev_ip]];
    NSURLResponse *response = nil;
    NSError *error = nil;

    NSData *connectionData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

您可以检查响应值和错误。



Answer 2:

有一个更好的方式来测试服务器是否有效。 该可达性类提供良好的API用于此目的。



文章来源: Checking for valid IP for connection with NSURLConnection