基本授权 - SSL 握手失败

basic Authorization - SSL Handshake failed

尝试使用基本授权通过 https 连接到网络服务,但我无法让它工作。我收到的错误消息:CFNetwork SSLHandshake 失败 (-9806)。感谢你的帮助。相关代码如下:

            var post : NSString = "username=\(username)&password=\(password)"
        let postData = post.dataUsingEncoding(NSUTF8StringEncoding)
        let base64EncodedCredential = postData!.base64EncodedStringWithOptions(nil)



        NSLog("PostData: %@",post);

        var url : NSURL = NSURL(string: "https://localhost:8080/webservice")!

        var postLength : NSString = String( base64EncodedCredential.lengthOfBytesUsingEncoding(NSUTF8StringEncoding) )

        var request:NSMutableURLRequest = NSMutableURLRequest(URL: url)
        request.HTTPMethod = "POST"
        request.HTTPBody = postData
        request.setValue("Basic \(base64EncodedCredential)", forHTTPHeaderField: "Authorization")
        request.setValue(postLength, forHTTPHeaderField: "Content-Length")
        request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
        request.setValue("application/json", forHTTPHeaderField: "Accept")


        let queue:NSOperationQueue = NSOperationQueue()
        let urlConnection = NSURLConnection(request: request, delegate: self, startImmediately: true)
        urlConnection!.start()

该问题与基本授权无关,因为这是在 SSL 握手成功后完成的。根据您 URL 的 https://localhost:8080 我建议您至少有以下问题之一,但可能有几个:

  • 没有使用 HTTPS,而是纯 HTTP。端口 8080 对于 HTTPS 来说非常不常见,但经常与 HTTP 一起使用。
  • 证书中的名称与 URL 中的名称不匹配 (localhost)。
  • 证书不是由客户端信任的机构签署的。

我建议使用浏览器检查 URL 以找到有关此问题的更多信息。