【问题标题】:basic Authorization - SSL Handshake failed基本授权 - SSL 握手失败
【发布时间】:2015-06-06 11:07:48
【问题描述】:

尝试使用基本授权通过 https 连接到 Web 服务,但是我无法让它工作。我收到的错误消息:CFNetwork SSLHandshake failed (-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()

【问题讨论】:

标签: swift ssl https


【解决方案1】:

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

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

我建议使用浏览器检查 URL,以了解有关该问题的更多信息。

【讨论】:

  • 是的,当我将其更改为 https 时,端口更改为 8181。谢谢!
猜你喜欢
  • 2017-10-24
  • 2018-07-28
  • 2012-10-11
  • 2015-03-16
  • 2021-12-28
  • 2018-10-17
  • 2016-04-29
  • 2014-03-05
相关资源
最近更新 更多