[英]Swift: How to Make Https Request Using Server SSL Certificate
Hi I want to make Https Request in Swift. 嗨,我想在Swift中进行Https请求。 Currently im accessing local server through ip address.
当前即时通讯通过IP地址访问本地服务器。 Local Server has one SSL Certificate by accessing the certificate want to make request to server currently im doing like this.
本地服务器通过访问要向当前正在这样做的服务器发出请求的证书来获得一个SSL证书。
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)")
}
}
I have used the above code for making request and in plist 我已经使用上面的代码进行请求并在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>
in plist i have given like this but im still getting error like 在plist中,我已经给出了这样的信息,但我仍然收到类似的错误
NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813)
IMPORTANT: DO NOT USE THIS IN PRODUCTION. 重要说明:请勿在生产中使用此产品。 Basically, with Alamofire, you can bypass the authentication for app development and testing purpose.
基本上,使用Alamofire,您可以绕过身份验证以进行应用程序开发和测试。 Make sure you remove it before the app is on the App Store or in production:-
确保在应用程序在App Store或正式生产中之前将其删除:-
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)
}
}
Thank You! 谢谢! Let me know if this helps.
让我知道是否有帮助。 :)
:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.