SwiftHTTP凭证和自签名证书

时间:2016-10-08 06:48:31

标签: certificate credentials trust

我正在使用SwiftHTTP并尝试从我的服务器获取JSON Feed。服务器具有自签名证书和通过用户和密码访问。

我尝试使用Alamofire,但我找不到解决方案

这是我的代码

   HTTP.globalRequest { req in
        req.timeoutInterval = 5
    }

    //set a global SSL pinning setting
    HTTP.globalSecurity(HTTPSecurity()) //see the SSL section for more info

    //set global auth handler. See the Auth section for more info
    HTTP.globalAuth { challenge in
        return URLCredential(user: "user", password: "passwd", persistence: .forSession)
    }


    do {
        let opt = try HTTP.GET("https://10.0.1.2:4711/fhem/?cmd=jsonlist2&XHR=1",requestSerializer: JSONParameterSerializer())
        //the auth closures will continually be called until a successful auth or rejection
        var attempted = false

        opt.auth = { challenge in
            if !attempted {
                attempted = true
                return URLCredential(forTrust: challenge.proposedCredential?.certificates)
            }
            return nil
        }

        opt.start { response in

            print("success")
            print("opt finished: \(response.data)")
        }
    } catch let error {
        print("got an error creating the request: \(error)")
    }



     }

任何想法?

0 个答案:

没有答案