基本授权 - SSL握手失败

问题描述:

试图通过基本授权通过https连接到web服务,但我无法让它工作。我收到的错误消息:CFNetwork SSLHandshake失败(-9806)。感谢你的帮助。这里是相关的代码:基本授权 - SSL握手失败

  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() 
+0

http://*.com/questions/25914248/ios-8-has-broken-ssl-connection-in-my-app-cfnetwork-sslhandshake-failed-9806 – 2015-04-01 18:00:05

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

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

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

+0

是的,当我将它改为https时端口改为8181.谢谢! – Jhonny 2015-04-01 18:27:26