gpt4 book ai didi

ios - AFNetworking SSL 证书失败 iOS

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:35:20 27 4
gpt4 key购买 nike

我花了很多时间寻找答案,但仍然没有运气,所以我决定放弃并寻求社区帮助:

1 我有一个带有自签名证书的开发服务器

2 是的,我确实使用了 policy.allowInvalidCertificates = YES;

3 SSL 在自定义端口 44302 上

4 我需要运行一个下载任务因为请求返回一个数据流

这是我的代码:

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
AFSecurityPolicy *policy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
policy.allowInvalidCertificates = YES;
manager.securityPolicy = policy;

NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"https://self_signed_cert.net:44302/storage/%@/action.download", _ticketId]];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
if (error) {
NSLog(@"Error: %@", error);
} else {
NSLog(@"%@ %@", response, responseObject);
}
}];
[dataTask resume];

这里是错误:

2015-08-03 18:18:15.901 SSL_TEST[1517:23943] Error: Error Domain=NSURLErrorDomain Code=-1200 "An SSL error has occurred and a secure connection to the server cannot be made." UserInfo=0x787d2f20 {NSLocalizedDescription=An SSL error has occurred and a secure connection to the server cannot be made., NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, _kCFStreamErrorCodeKey=-9806, NSErrorFailingURLStringKey=https://self_signed_cert.net:44302/storage/a430f316cfe076e9850874c0edad3dcb/action.download, _kCFStreamErrorDomainKey=3, NSUnderlyingError=0x787da700 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -1200.)", NSErrorFailingURLKey=https://self_signed_cert.net:44302/storage/a430f316cfe076e9850874c0edad3dcb/action.download}

非常感谢您的帮助!谢谢。

最佳答案

我试过一个空项目,它可以在应用程序上运行:

NSURL* lBaseURL = [NSURL URLWithString:@"https://myDomain/"];
AFHTTPSessionManager* lSessionManager = [[AFHTTPSessionManager alloc] initWithBaseURL:lBaseURL
sessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];


AFSecurityPolicy* lPolicy= [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
lPolicy.allowInvalidCertificates = YES;

lSessionManager.securityPolicy = lPolicy;


[lSessionManager GET:@"https://myDomain/api/"
parameters:nil
success:^(NSURLSessionDataTask * _Nonnull task, id _Nonnull JSON)
{
NSLog(@"success %@", JSON);
}
failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error)
{
NSLog(@"failure %@", error);
}];
  • 不要忘记将您的 .cer 文件添加到您的目标(在应用程序和 watch 应用程序中)
  • 激活 ATS 到 .plist NSAppTransportSecurity

但我坚持使用 watchOS 2.0 扩展。它只是失败并出现此错误:

Error Domain=NSURLErrorDomain Code=-1200 "An SSL error has occurred and a secure connection to the server cannot be made." UserInfo={NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, NSErrorFailingURLKey=https://myDomain/api, _kCFStreamErrorCodeKey=-9802, NSErrorFailingURLStringKey=https://myDomain/api, NSErrorClientCertificateStateKey=0, _kCFStreamErrorDomainKey=3, NSLocalizedDescription=An SSL error has occurred and a secure connection to the server cannot be made.}

关于ios - AFNetworking SSL 证书失败 iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31791290/

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