小弟今天用AFNetworking2.0向内测服务器发送https网络请求,结果出现了错误
错误内容大致如下:
NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802)
Error Domain=NSURLErrorDomain Code=-1200 "An SSL error has occurred and a secure connection to the server cannot be made." UserInfo={NSURLErrorFailingURLPeerTrustErrorKey=<SecTrustRef: 0x7f82b3d13460>, NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, _kCFStreamErrorDomainKey=3, _kCFStreamErrorCodeKey=-9802, NSErrorPeerCertificateChainKey=<CFArray 0x7f82b3d29080 [0x108fce7b0]>{type = immutable, count = 1, values = (
0 : <cert(0x7f82b3dc8620) s: 10.10.120.194 i: 10.10.120.194>
)}, NSUnderlyingError=0x7f82b3ca07f0 {Error Domain=kCFErrorDomainCFNetwork Code=-1200 "An SSL error has occurred and a secure connection to the server cannot be made." UserInfo={NSErrorFailingURLStringKey=https://10.10.120.194/mobile/login, NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, _kCFNetworkCFStreamSSLErrorOriginalValue=-9802, kCFStreamPropertySSLPeerCertificates=<CFArray 0x7f82b3d29080 [0x108fce7b0]>{type = immutable, count = 1, values = (
0 : <cert(0x7f82b3dc8620) s: 10.10.120.194 i: 10.10.120.194>
)}, _kCFStreamPropertySSLClientCertificateState=0, kCFStreamPropertySSLPeerTrust=<SecTrustRef: 0x7f82b3d13460>, NSLocalizedDescription=An SSL error has occurred and a secure connection to the server cannot be made., _kCFStreamErrorDomainKey=3, NSErrorFailingURLKey=https://10.10.120.194/mobile/login, _kCFStreamErrorCodeKey=-9802}}, NSLocalizedDescription=An SSL error has occurred and a secure connection to the server cannot be made., NSErrorFailingURLKey=https://10.10.120.194/mobile/login, NSErrorFailingURLStringKey=https://10.10.120.194/mobile/login, NSErrorClientCertificateStateKey=0}
我试过了网上说的,在plist中添加属性的方法,发现也解决不了。同时,我也在网络请求时忽略了非法证书,但是问题还是存在。
于是,我再次怀疑这是不是由于xcode7导致的,结果我把它拿到低版本的xcode中去运行,结果完美运行,我真的不知道该咋解决了,求大神指点。
代码如下所示:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.securityPolicy.allowInvalidCertificates = YES;
NSDictionary *parameters = @{
@“pare1”:userId,
@“pare2”:tmpPassword.stringFromMD5,
@“parenum”:@1
};
[manager POST:serverAddress parameters:parameters
success:^(AFHTTPRequestOperation *operation, id responseObject) {
_hasNetErrorHappened = NO;
//block回调
success(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
_hasNetErrorHappened = YES;
//block回调
failure(error);
}];
4 个解决方案
#1
你改了配置之后,就不要用https请求,用http请求试试
#2
同样的问题,xcode7 环境下不该配置文件的情况下, AFNETWORKING 访问https 配置allowInvalidCertificates = YES; 报错一模一样。楼主解决了吗
#3
我的解决办法很奇怪,我设置了plist,后来居然好使了。
#4
添加哪两个属性啊?
#1
你改了配置之后,就不要用https请求,用http请求试试
#2
同样的问题,xcode7 环境下不该配置文件的情况下, AFNETWORKING 访问https 配置allowInvalidCertificates = YES; 报错一模一样。楼主解决了吗
#3
我的解决办法很奇怪,我设置了plist,后来居然好使了。
#4
添加哪两个属性啊?