gpt4 book ai didi

swift - 带有不受信任的 SSL 证书的 HTTPS 站点在 swift 中的自动登录问题

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

我在使用不受信任的 SSL 证书快速登录 HTTPS 站点时遇到困难。我可以使用 NSURLConnectionDelegate 方法在我的 UIWebView 中查看页面。我可以使用网络浏览器自动登录该网站,例如:URL - https://mywebsite.com:8003/home.html?session_id=sessionVariable .但不是来自 UIWEbView

这是我在 ViewDidLoad 中的代码:

 let websiteURL = "https://mywebsite.com:8003/home.html?session_id=\(Session_Id)"

print(websiteURL)
let url = NSURL (string: websiteURL)
let urlRequest = NSURLRequest(URL: url!)
let urlConnection:NSURLConnection = NSURLConnection(request: urlRequest, delegate: self)!
self.webviewInstance.loadRequest(urlRequest)

我的委托(delegate)函数是

func connection(connection: NSURLConnection, canAuthenticateAgainstProtectionSpace protectionSpace: NSURLProtectionSpace) -> Bool{
print("canAuthenticateAgainstProtectionSpace method Returning True")
return true
}


func connection(connection: NSURLConnection, didReceiveAuthenticationChallenge challenge: NSURLAuthenticationChallenge){

print("did autherntcationchallenge = \(challenge.protectionSpace.authenticationMethod)")

if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {
print("send credential Server Trust")
let credential = NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!)
challenge.sender!.useCredential(credential, forAuthenticationChallenge: challenge)

}else if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodHTTPBasic{
print("send credential HTTP Basic")
let defaultCredentials: NSURLCredential = NSURLCredential(user: "username", password: "password", persistence:NSURLCredentialPersistence.ForSession)
challenge.sender!.useCredential(defaultCredentials, forAuthenticationChallenge: challenge)

}else if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodNTLM{
print("send credential NTLM")

} else{
challenge.sender!.performDefaultHandlingForAuthenticationChallenge!(challenge)
}
}

我已经在我的 plist 文件中添加了 key 的更改NSAppTransportSecurity , NSAllowsArbitraryLoads 等

我可以通过此服务器与 JSON 调用进行交互。但是从 webView 自动登录不起作用。请指教。

最佳答案

除了您自己创建的请求外,不会调用这些委托(delegate)方法,而且我认为 cookie 也不会在 WebView 和普通应用程序请求之间共享。

你最好的选择是创建一个自定义的 NSURLProtocol 来拦截 http/https 请求(来自 WebView 和其他地方),添加一个自定义 header (这样你就可以在你的协议(protocol)类再次看到它们时识别重新发送的请求)然后重新发送使用您自己的 NSURLConnection 对象的请求。

有关详细信息,请参阅 Using a custom URL scheme with UIWebView

关于swift - 带有不受信任的 SSL 证书的 HTTPS 站点在 swift 中的自动登录问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38012757/

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