使用NSURLSession连接到具有无效证书的服务器(swift2,xcode7,ios9)

使用NSURLSession连接到具有无效证书的服务器(swift2,xcode7,ios9),第1张

概述我正在使用 Xcode 7, Swift 2和iOS9.我想使用NSURLSession连接到Web服务,但是当我尝试连接时出现以下错误: 2015-10-13 16:07:33.595 XCTRunner[89220:4520715] NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813)20 @H_404_1@ 我正在使用 Xcode 7,Swift 2和iOS9.我想使用NSURLSession连接到Web服务,但是当我尝试连接时出现以下错误:

2015-10-13 16:07:33.595 XCTRunner[89220:4520715] NSURLSession/NSURLConnection http load Failed (kcfStreamErrorDomainSSL,-9813)2015-10-13 16:07:33.604 XCTRunner[89220:4520571] Error with connection,details: Error Domain=NSURLErrorDomain Code=-1202 "The certificate for this server is invalID. You might be connecting to a server that is pretending to be “domainAPI.com” which Could put your confIDential information at risk." UserInfo={NSURLErrorFailingURLPeerTrustErrorKey=<SecTrustRef: 0x7fac7b6facc0>,NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?,

这是我的代码:

func request( dataPost : String,successHandler: (response: String) -> VoID)-> String {        let destination:String =  "https://domainAPI.com:8743/WebService/sendData"        let request = NSMutableURLRequest(URL: NSURL(string: destination as String)!)        request.httpMethod = "POST"        let poststring = dataPost        request.httpBody = poststring.dataUsingEnCoding(NSUTF8StringEnCoding)        request.setValue("0",forhttpheaderFIEld: "Content-Length")        request.setValue("application/xml",forhttpheaderFIEld: "Content-Type")        request.setValue("gzip,deflate",forhttpheaderFIEld: "Accept-EnCoding")        request.setValue("Keep-Alive",forhttpheaderFIEld: "Connection")        NSLog("Body is: %@",request.httpBody!)        NSLog("Request is: %@",request.allhttpheaderFIElds!)        NSLog("URL is: %@",destination)        let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {            data,response,error in            if error != nil {                NSLog("Error with connection,details: %@",error!)                return            }            let responseString = Nsstring(data: data!,enCoding: NSUTF8StringEnCoding)            successHandler(response: responseString as String!);            NSLog("Data received: %@",data!)        }        task.resume()        return "worked"    }    func vIEwDIDLoad() {        let dataPost : String = "<webservices>xml data sending</webservices>"        request(dataPost,successHandler: {            (response) in            let text = response            print(text)        });

我已经研究过NSURLAuthenticationChallenge,但我似乎无法用我现有的代码来解决这个问题.所以我的问题是如何才能连接到服务器?我已经尝试将域添加到Info.pList中的NSAppTransportSecurity但是没有用.打开NSAllowsArbitraryLoads也不起作用.任何帮助,将不胜感激.

解决方法 请看一下这篇文章.7000,特别是有关自签名证书的部分.

你很可能需要表单的委托方法,

func URLSession(session: NSURLSession,dIDReceiveChallenge challenge: NSURLAuthenticationChallenge,completionHandler: (NSURLSessionAuthChallengedisposition,NSURLCredential?) -> VoID) {    completionHandler(        .UseCredential,NSURLCredential(trust: challenge.protectionSpace.serverTrust!)    )}

将此添加到我自己的使用NSURLSession的comms类修复了该问题.

总结

以上是内存溢出为你收集整理的使用NSURLSession连接到具有无效证书的服务器(swift2,xcode7,ios9)全部内容,希望文章能够帮你解决使用NSURLSession连接到具有无效证书的服务器(swift2,xcode7,ios9)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1002652.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-22
下一篇2022-05-22

发表评论

登录后才能评论

评论列表(0条)

    保存