NEHotspotHelper:无法在身份验证状态机的身份验证状态下发送 Web 请求

NEHotspotHelper: Not Able to Send Web Request in Authenticating State of Authentication State Machine

我已经实现了 NEHotspotHelper,这样我就可以在后台为带有强制门户的网络执行身份验证。

我需要在 'authenticating' 状态下执行网络请求,以便我可以检索 Wispr 并访问 API。

但是,当我尝试使用 URLSession 发送网络请求时,请求失败了。这是错误:

[594:88737] dnssd_clientstub read_all(7) DEFUNCT
[594:88783] TIC TCP Conn Failed [4:0x1c0177a00]: 12:8 Err(-65554)
[594:88783] Task <FFD0DAE6-4864-437D-94F2-C9ED5D5748E2>.<1> HTTP load failed (error code: -1003 [12:8])
[594:88783] Task <FFD0DAE6-4864-437D-94F2-C9ED5D5748E2>.<1> finished with error - code: -1003

查看我的代码片段:


 let registered = NEHotspotHelper.register(options: options, queue: queue) { (cmd: NEHotspotHelperCommand) in
    print("Received command: \(cmd.commandType.rawValue)")
    if cmd.commandType == NEHotspotHelperCommandType.filterScanList {
        //Get all available hotspots
        print("filter scan list")
        var list: [NEHotspotNetwork] = cmd.networkList!
        var response: NEHotspotHelperResponse
        for l in list {
            if (l.ssid=="my-ssid") {
                response = cmd.createResponse(NEHotspotHelperResult.success)
            } else {
                response = cmd.createResponse(NEHotspotHelperResult.failure)                       
            }
            response.setNetworkList([chosenNetwork])
            response.deliver()
        }   
    } else if cmd.commandType == NEHotspotHelperCommandType.evaluate {
        if let network = cmd.network {            
            if (network.ssid=="my-ssid") {            
                network.setConfidence(NEHotspotHelperConfidence.high)
                let response = cmd.createResponse(NEHotspotHelperResult.success)
                response.setNetwork(network)                 
                response.deliver() //Respond back
            } else {
                let response = cmd.createResponse(NEHotspotHelperResult.failure)
                response.deliver()
            }
        }
    } else if cmd.commandType == NEHotspotHelperCommandType.authenticate {
        print("authenticate")
        var response = cmd.createResponse(NEHotspotHelperResult.unsupportedNetwork)
        if let network = cmd.network{
            if network.ssid == "my-ssid"{
                self.queryUrl()
                response = cmd.createResponse(NEHotspotHelperResult.success)
            }
        }
        response.deliver() //Respond back                    
    }
}

func queryUrl(){
    let config = URLSessionConfiguration.default
    config.allowsCellularAccess = false;

    let session = URLSession.init(configuration: config)

    let url = URL(string: "https://172.217.20.35")

    let semaphore = DispatchSemaphore(value: 0)
    let task = session.dataTask(with: url!){(data, response, error) in
        if  data==nil {
            print(data as Any)            
        }
        else{
            print(NSString(data: data!, encoding: String.Encoding.utf8.rawValue) as Any)
        }
        semaphore.signal()
    }

    task.resume()
    _ = semaphore.wait(timeout: .distantFuture)    
}

仅此而已:

if cmd.commandType
    filterScanList
        - SetConfidence to each of your networks in cmd.networkList - High and add to a local list variable.
        - create Response.Success
        - set Response.NetworkList to your list of confident networks
        ----
        - deliver Response
    Evaluate or PresentUI
        //If it's your network
        - SetConfidence to cmd.network - High
        - create Response.Success
        - set Response.Network to cmd.network
        //If it's not your network
        - create Response.UnsupportedNetwork
        ----
        - deliver Response
    Authenticate or Maintain
        //If it's your network
        - SetConfidence to cmd.network - High
        - create Response.Success
        - set Response.Network to cmd.network
        - TRY AUTHENTICATE HERE SYNCHRONOUSLY
        //If it's not your network
        - create Response.UnsupportedNetwork
        ----
        - deliver Response
    Logoff or None
        - create Response.Success
        ----
        - deliver Response

其他人报告了来自 NEHotspotHelper 回调的名称服务器解析问题。尝试使用 IP 地址拨打电话。

另外不要忘记 URLSession 是异步工作的。只有在 Web 服务调用完成后,您才需要调用 response.deliver()

我也遇到了类似的问题。但是,我发现开发人员在向连接的网络发出 Web 请求之前需要将请求与接收到的命令绑定。您需要做的就是制作NSMutableURLRequest,然后调用hitTestURLRequest.bind(to: command),因为绑定函数定义在NSMutableURLRequest.

的类别中