NeHotspotHelper:无法在身份验证状态机的身份验证状态下发送Web请求 [英] NeHotspotHelper: not able to send web request in authenticating state of authentication state machine

查看:318
本文介绍了NeHotspotHelper:无法在身份验证状态机的身份验证状态下发送Web请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了NeHotspotHelper,以便可以在具有强制门户的网络的后台执行身份验证.

I have implemented the NeHotspotHelper so that I can perform authentication in the background for networks with a captive portal.

我需要在身份验证"状态下执行网络请求,以便我可以检索Wispr并访问API.

I need to perform a web request in the 'authenticating' state so that I can retrieve the Wispr and also access an API.

但是,当我尝试使用URLSession发送Web请求时,请求失败.这是错误:

However, when I try to use URLSession to send a web request, the request fails. This is the error:

[594:88737] dnssd_clientstub read_all(7)禁用 [594:88783] TIC TCP连接失败[4:0x1c0177a00]:12:8 Err(-65554) [594:88783]任务< 1> HTTP加载失败(错误代码:-1003 [12:8]) [594:88783]任务< 1>完成,错误-代码:-1003

[594:88737] dnssd_clientstub read_all(7) DEFUNCT [594:88783] TIC TCP Conn Failed [4:0x1c0177a00]: 12:8 Err(-65554) [594:88783] Task .<1> HTTP load failed (error code: -1003 [12:8]) [594:88783] Task .<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)

    }

推荐答案

我也面临着类似的问题.但是,我发现开发人员需要在将Web请求发送到连接的网络之前将请求与接收到的命令绑定.您要做的就是先创建NSMutableURLRequest,然后调用hitTestURLRequest.bind(to: command),因为bind函数是在NSMutableURLRequest类别中定义的.

I was also facing the similar issue. However, I found that developer need to bind the request with the received command before making web request to the connected network. All you need to do is to make NSMutableURLRequest and then call hitTestURLRequest.bind(to: command) because bind function is defined in the category of NSMutableURLRequest.

这篇关于NeHotspotHelper:无法在身份验证状态机的身份验证状态下发送Web请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆