尝试使用基本授权通过 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 握手成功后完成的。根据您的 https://localhost:8080
URL,我建议您至少有以下问题之一,但也可能有几个:
- 不使用 HTTPS,而是纯 HTTP。端口 8080 对于 HTTPS 来说非常不常见,但通常与 HTTP 一起使用。
- 证书中的名称与 URL 中的名称不匹配 (
localhost
)。 - 证书未由客户信任的机构签署。
我建议使用浏览器检查 URL 以查找有关该问题的更多信息。
关于swift - 基本授权 - SSL 握手失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29396953/