Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json 如何在iOS9上进行http基本身份验证?_Json_Ssl_Swift2_Xcode7_Basic Authentication - Fatal编程技术网

Json 如何在iOS9上进行http基本身份验证?

Json 如何在iOS9上进行http基本身份验证?,json,ssl,swift2,xcode7,basic-authentication,Json,Ssl,Swift2,Xcode7,Basic Authentication,我一直试图通过http基本身份验证将ios应用程序连接到restful web API,但无法连接。这是我的密码: let URL = NSURL(string:"https://devWebsvc1.whateverYolo.local:11201/api/webcall") let theRequest = NSMutableURLRequest(URL:URL) let session = NSURLSession(configuration: NSURLSessionConfigurat

我一直试图通过http基本身份验证将ios应用程序连接到restful web API,但无法连接。这是我的密码:

let URL = NSURL(string:"https://devWebsvc1.whateverYolo.local:11201/api/webcall")
let theRequest = NSMutableURLRequest(URL:URL)
let session = NSURLSession(configuration: NSURLSessionConfiguration.ephemeralSessionConfiguration())
theRequest.HTTPMethod = "POST"
theRequest.addValue("application/json", forHTTPHeaderField:"Content-Type")
theRequest.addValue("application/json", forHTTPHeaderField:"Accept")


let credential = NSURLCredential(user:"username", password:"password", persistence: NSURLCredentialPersistence.ForSession)
let protectionSpace = NSURLProtectionSpace(Host: URL?.host)!, port:11201, 'protocol': URL?.scheme, realm: nil, authenticationMethod: NSURLAuthenticationMethodHTTPBasic)
let credentialStorage = NSURLCredentialStorage.sharedCredentialStorage()
credentialStorage.setDefaultCredential(credential,forProtectionSpace:protectionSpace)
theSession.configuration.URLCredentialsStorage = credentialStorage

let task = theSession.dataTaskWithRequest(theRequest, completionHandler : {data, response, error -> Void in 
if error != nil
{print("\(error)")}})
错误消息是:-

OptionalError Domain=nsurErrorDomain Code=-1200发生SSL错误,无法与服务器建立安全连接。UserInfo={NSURlerErrorFailingUrlPeerTrustErrorKey=,NSLocalizedRecoverysSuggestion=是否仍要连接到服务器?,kCFStreamErrorDomainKey=3,kCFStreamErrorCodeKey=-9802,NSErrorPeerCertificateChainKey={type=不可变,计数=1,值= 0 : },NSUnderlyingError=0x7fe023520170{Error Domain=kCFErrorDomainCFNetwork Code=-1200 null UserInfo={kCFStreamPropertySSLClientCertificateState=0,kCFStreamPropertySSLPeerTrust=,KCFnetworkCfstreamPropertySLRoriginalValue=-9802,kCFStreamErrorDomainKey=3,kCFStreamErrorCodeKey=-9802,KCFStreamPropertySSLpeertificates={type=不可变,count=1,value= 0 : }}},NSLocalizedDescription=发生SSL错误,无法与服务器建立安全连接,,,NSErrorFailingURLKey=,NSErrorFailingURLStringKey=,NSErrorClientCertificateStateKey=0}


你们知道会出现什么错误吗?非常感谢你们的帮助。顺便说一下,我正在使用xcode 7。

错误可能在这行:-

theRequest.addValue("application/json", forHTTPHeaderField:"Accept")
你应该转到这个google扩展,找到Accept字段的值是多少

铬-extension://hgmloofddffdnphfgcellkdfbfbjeloo/RestClient.html

添加您的url、用户名和密码,然后单击“提交”,发现所有标题字段都是正确的,如下图所示,同时检查内容类型的值


您的服务器是否处理TLS 1.2安全协议?这在iOS 9中是必需的。如果这是一个问题,您可以在此处查找解决方案。