gpt4 book ai didi

ios - Swift 3 使用无效的 XML 证书连接到服务器

转载 作者:太空宇宙 更新时间:2023-11-03 14:40:25 26 4
gpt4 key购买 nike

我在连接到服务器时遇到了一些问题。我只想发送用户名和密码并从 XML 文件中获取一些数据。

    let loginString = String(format: "%@:%@", username, password)
let loginData = loginString.data(using: String.Encoding.utf8)!
let base64LoginString = loginData.base64EncodedString()

let baseUrl = "xxxxxx"
let request = NSMutableURLRequest(url: NSURL(string: baseUrl)! as URL)
request.httpMethod = "POST"
request.setValue("Basic \(base64LoginString)", forHTTPHeaderField: "Authorization")
let session = URLSession.shared
request.httpMethod = "GET"

var err: NSError?

let task = session.dataTask(with: request as URLRequest) {
(data, response, error) in

if data == nil {
print("dataTaskWithRequest error: \(error)")
return
}

let xml = SWXMLHash.parse(data!)
print(xml["mobile_devices"]["mobile_device"]["serial_number"].element?.text)

DispatchQueue.main.async(execute: {
// use main thread for UI updates
})

}
task.resume()

当我运行它时,出现以下错误:

nw_coretls_callback_handshake_message_block_invoke_3 tls_handshake_continue: [-9812] 2017-05-04 08:20:45.311 xxxx[23410:4428038] NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813)

NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813) dataTaskWithRequest error: Optional(Error Domain=NSURLErrorDomain Code=-1202 "The certificate for this server is invalid. You might be connecting to a server that is pretending to be “xxxxxxx” which could put your confidential information at risk." UserInfo={NSURLErrorFailingURLPeerTrustErrorKey=, NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, _kCFStreamErrorDomainKey=3, _kCFStreamErrorCodeKey=-9813, NSErrorPeerCertificateChainKey=("cert(0x7f88d8834000) s: xxxxxxx JSS Built-in Certificate Authority>",

我以前从未真正使用过 API 和其他东西,所以我边学边学。以前我必须编辑 plist 文件以允许连接,这解决了我之前遇到的问题。我已经走了这么远,但我不确定如何克服它。

我看到了这个:Connect to a Server with Invalid Certificate using NSURLSession (swift2,xcode7,ios9) 但它打破了

let session = URLSession.shared 

我的代码中的行。

我也看过这个,但我不确定如何使用它:

Swift: How to request a URL with a self-signed certificate?

最佳答案

好的。我找到了适合我自己情况的解决方案。我把它贴在这里:Swift 3 NSURLSession/NSURLConnection HTTP load failed when connecting to a site with authentication

注意:这是一个安全问题,所以请不要这样做

关于ios - Swift 3 使用无效的 XML 证书连接到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43759373/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com