Ios Swift:如何使用服务器SSL证书发出Https请求
嗨,我想用Swift发出Https请求。当前im通过ip地址访问本地服务器。通过访问证书,本地服务器有一个SSL证书。要向服务器发出请求,当前我正在这样做Ios Swift:如何使用服务器SSL证书发出Https请求,ios,swift,ssl,https,Ios,Swift,Ssl,Https,嗨,我想用Swift发出Https请求。当前im通过ip地址访问本地服务器。通过访问证书,本地服务器有一个SSL证书。要向服务器发出请求,当前我正在这样做 Alamofire.request(.GET, https://ipaddress//, parameters: [param], headers: headers) .responseJSON { response in print(response.request) // original U
Alamofire.request(.GET, https://ipaddress//, parameters: [param], headers: headers)
.responseJSON { response in
print(response.request) // original URL request
print(response.response) // URL response
print(response.data) // server data
print(response.result) // result of response serialization
if let JSON = response.result.value {
print("JSON: \(JSON)")
}
}
我已经在plist中使用了上述代码来发出请求
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>192.168.2.223:1021(my local ip)</key>
<dict>
Include to allow subdomains-->
<key>NSIncludesSubdomains</key>
<true/>
<!--Include to allow insecure HTTP requests-->
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
<!--Include to specify minimum TLS version-->
<key>NSTemporaryExceptionMinimumTLSVersion</key>
<string>TLSv1.1</string>
</dict>
</dict>
</dict>
重要提示:请勿在生产中使用此功能。基本上,使用Alamofire,您可以绕过身份验证进行应用程序开发和测试。确保在应用程序进入应用商店或投入生产之前将其删除:-
func bypassURLAuthentication() {
let manager = Alamofire.Manager.sharedInstance
manager.delegate.sessionDidReceiveChallenge = { session, challenge in
var disposition: NSURLSessionAuthChallengeDisposition = .PerformDefaultHandling
var credential: NSURLCredential?
if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {
disposition = NSURLSessionAuthChallengeDisposition.UseCredential
credential = NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!)
} else {
if challenge.previousFailureCount > 0 {
disposition = .CancelAuthenticationChallenge
} else {
credential = manager.session.configuration.URLCredentialStorage?.defaultCredentialForProtectionSpace(challenge.protectionSpace)
if credential != nil {
disposition = .UseCredential
}
}
}
return (disposition, credential)
}
}
谢谢大家!!
如果有帮助,请告诉我。:) 重要提示:请勿在生产中使用此功能。基本上,使用Alamofire,您可以绕过身份验证进行应用程序开发和测试。确保在应用程序进入应用商店或投入生产之前将其删除:-
func bypassURLAuthentication() {
let manager = Alamofire.Manager.sharedInstance
manager.delegate.sessionDidReceiveChallenge = { session, challenge in
var disposition: NSURLSessionAuthChallengeDisposition = .PerformDefaultHandling
var credential: NSURLCredential?
if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {
disposition = NSURLSessionAuthChallengeDisposition.UseCredential
credential = NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!)
} else {
if challenge.previousFailureCount > 0 {
disposition = .CancelAuthenticationChallenge
} else {
credential = manager.session.configuration.URLCredentialStorage?.defaultCredentialForProtectionSpace(challenge.protectionSpace)
if credential != nil {
disposition = .UseCredential
}
}
}
return (disposition, credential)
}
}
谢谢大家!!
如果有帮助,请告诉我。:) 您不能在ATS plist值中使用IP地址。您得到解决方案了吗?您不能在ATS plist值中使用IP地址。您得到解决方案了吗?