gpt4 book ai didi

json - 如何在 iOS9 上做 http 基本认证?

转载 作者:太空宇宙 更新时间:2023-11-03 14:52:42 25 4
gpt4 key购买 nike

我一直在尝试通过 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)")}})

错误信息是:-

Optional(Error Domain=NSURLErrorDomain Code=-1200 "An SSL error has occurred and a secure connection to the server cannot be made." UserInfo={NSURLErrorFailingURLPeerTrustErrorKey=, NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, _kCFStreamErrorDomainKey=3, _kCFStreamErrorCodeKey=-9802, NSErrorPeerCertificateChainKey={type = immutable, count = 1, values = ( 0 : )}, NSUnderlyingError=0x7fe023520170 {Error Domain=kCFErrorDomainCFNetwork Code=-1200 "(null)" UserInfo={_kCFStreamPropertySSLClientCertificateState=0, kCFStreamPropertySSLPeerTrust=, _kCFNetworkCFStreamSSLErrorOriginalValue=-9802, _kCFStreamErrorDomainKey=3, _kCFStreamErrorCodeKey=-9802, kCFStreamPropertySSLPeerCertificates={type = immutable, count = 1, values = ( 0 : )}}}, NSLocalizedDescription=An SSL error has occurred and a secure connection to the server cannot be made., NSErrorFailingURLKey=https://devwebsvc1.whateverYolo.local:11201/api/Device, NSErrorFailingURLStringKey=https://devwebsvc1.whateverYolo.local:11201/api/Device, NSErrorClientCertificateStateKey=0})

你们知道可能是什么错误吗?任何帮助表示赞赏。顺便说一句,我正在使用 xcode 7。

最佳答案

错误可能在这一行:-

theRequest.addValue("application/json", forHTTPHeaderField:"Accept")

你应该去这个谷歌扩展并找到字段“接受”的值是什么

chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo/RestClient.html

enter image description here

添加你的 url,用户名和密码,然后点击提交,发现你所有的 Header Field 都是这样的,同时检查 Content-Type 的值

enter image description here

关于json - 如何在 iOS9 上做 http 基本认证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32829652/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com