AFNetworking 3 问题
AFNetworking 3 issue
在 AFNetworking
3 中,对于无效的 SSL 证书,我使用了 validatesCertificateChain = false ,但现在似乎该字段已被删除,我无法向我的服务器。
这里是class请求:
import UIKit
import AFNetworking
class ClientHTML: AFHTTPSessionManager {
private static var __once: () = { () -> Void in
let securityPolicy = AFSecurityPolicy(pinningMode: AFSSLPinningMode.certificate)
securityPolicy.validatesDomainName = false
securityPolicy.allowInvalidCertificates = true
sharedInstanceTemp.securityPolicy = securityPolicy
sharedInstanceTemp.requestSerializer = AFHTTPRequestSerializer()
sharedInstanceTemp.responseSerializer = AFHTTPResponseSerializer()
}()
fileprivate static let sharedInstanceTemp = ClientHTML(baseURL: URL(string: kServer_urlBilderlings))
static var sharedInstance:ClientHTML {
get {
_ = ClientHTML.__once
return sharedInstanceTemp
}
}
}
我通过以下方式请求:
ClientHTML.sharedInstance.post("https://acs-web-test.firstdata.lv", parameters: nil, progress: { (progress) in
print("progress = ", progress)
}, success: { (task, response) in
let data = response as! Data
let html = String(data: data, encoding: .utf8)
print("success responce = ", html)
}, failure: { (task, error) in
print("error = ", error)
})
并且根据 我必须使用 validatesCertificateChain
属性。有人可以帮忙吗?
我把let securityPolicy = AFSecurityPolicy(pinningMode: AFSSLPinningMode.certificate)
改成let securityPolicy = AFSecurityPolicy(pinningMode: AFSSLPinningMode.none)
解决了
没有 SSL Pinning 的 objective-c 用户 setAllowInvalidCertificates:YES
:
AFSecurityPolicy *sec = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
[sec setAllowInvalidCertificates:YES];
[sec setValidatesDomainName:NO];
manager.securityPolicy = sec;
Objective-C
static NSString* const kSecurityCertificate = @"xxxxx";
yourManager.securityPolicy = [self customSecurityPolicy];
- (AFSecurityPolicy*)customSecurityPolicy {
AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
NSString *cerPath = [[NSBundle mainBundle] pathForResource:kSecurityCertificate ofType:@"der"];
NSData *certData = [NSData dataWithContentsOfFile:cerPath];
[securityPolicy setValidatesDomainName:NO];
[securityPolicy setAllowInvalidCertificates:YES];
[securityPolicy setPinnedCertificates:[NSSet setWithObjects:certData, nil]];
return securityPolicy;
}
在 AFNetworking
3 中,对于无效的 SSL 证书,我使用了 validatesCertificateChain = false ,但现在似乎该字段已被删除,我无法向我的服务器。
这里是class请求:
import UIKit
import AFNetworking
class ClientHTML: AFHTTPSessionManager {
private static var __once: () = { () -> Void in
let securityPolicy = AFSecurityPolicy(pinningMode: AFSSLPinningMode.certificate)
securityPolicy.validatesDomainName = false
securityPolicy.allowInvalidCertificates = true
sharedInstanceTemp.securityPolicy = securityPolicy
sharedInstanceTemp.requestSerializer = AFHTTPRequestSerializer()
sharedInstanceTemp.responseSerializer = AFHTTPResponseSerializer()
}()
fileprivate static let sharedInstanceTemp = ClientHTML(baseURL: URL(string: kServer_urlBilderlings))
static var sharedInstance:ClientHTML {
get {
_ = ClientHTML.__once
return sharedInstanceTemp
}
}
}
我通过以下方式请求:
ClientHTML.sharedInstance.post("https://acs-web-test.firstdata.lv", parameters: nil, progress: { (progress) in
print("progress = ", progress)
}, success: { (task, response) in
let data = response as! Data
let html = String(data: data, encoding: .utf8)
print("success responce = ", html)
}, failure: { (task, error) in
print("error = ", error)
})
并且根据 validatesCertificateChain
属性。有人可以帮忙吗?
我把let securityPolicy = AFSecurityPolicy(pinningMode: AFSSLPinningMode.certificate)
改成let securityPolicy = AFSecurityPolicy(pinningMode: AFSSLPinningMode.none)
解决了
没有 SSL Pinning 的 objective-c 用户 setAllowInvalidCertificates:YES
:
AFSecurityPolicy *sec = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
[sec setAllowInvalidCertificates:YES];
[sec setValidatesDomainName:NO];
manager.securityPolicy = sec;
Objective-C
static NSString* const kSecurityCertificate = @"xxxxx";
yourManager.securityPolicy = [self customSecurityPolicy];
- (AFSecurityPolicy*)customSecurityPolicy {
AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
NSString *cerPath = [[NSBundle mainBundle] pathForResource:kSecurityCertificate ofType:@"der"];
NSData *certData = [NSData dataWithContentsOfFile:cerPath];
[securityPolicy setValidatesDomainName:NO];
[securityPolicy setAllowInvalidCertificates:YES];
[securityPolicy setPinnedCertificates:[NSSet setWithObjects:certData, nil]];
return securityPolicy;
}