I'm having some issues with a connection to a server. I want to simply send a username and password and get some data from an XML file.
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()
When I run it, I get the following error(s):
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>",
I've never really done anything with APIs and things before, so I'm learning as I go. Previously I had to edit the plist file to allow the connection, and that fixed a problem I had before. I've got this far but I'm not sure how to get past it.
I saw this: Connect to a Server with Invalid Certificate using NSURLSession (swift2,xcode7,ios9) but it broke the
let session = URLSession.shared
line in my code.
I've seen this as well, but I'm unsure how to use it: