gpt4 book ai didi

ios - NSURLSession + 带有自签名证书的服务器

转载 作者:IT王子 更新时间:2023-10-29 07:48:42 25 4
gpt4 key购买 nike

我有一个生产应用程序以及一个具有自签名证书的开发服务器。

我正在尝试测试 NSURLSession 和后台下载,但似乎无法通过 - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler: (void (^)(NSURLSessionAuthChallengeDisposition 处置, NSURLCredential *credential))completionHandler

当我使用 NSURLConnection 时,我可以使用以下方法绕过它:

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {

NSLog(@"canAuthenticateAgainstProtectionSpace %@", [protectionSpace authenticationMethod]);

return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {

NSLog(@"didReceiveAuthenticationChallenge %@ %zd", [[challenge protectionSpace] authenticationMethod], (ssize_t) [challenge previousFailureCount]);

[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
}

但我不知道如何让它与 NSURLSession 一起工作 >:(

这是我目前拥有的(不起作用):

- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler {
NSLog(@"NSURLSession did receive challenge.");

completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
}

我还尝试创建一个 NSURLSession 类别,它允许主机使用任何证书:

#import "NSURLRequest+IgnoreSSL.h"

@implementation NSURLRequest (IgnoreSSL)

+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString*)host {
return YES;
}

+ (void)setAllowsAnyHTTPSCertificate:(BOOL)allow forHost:(NSString*)host {}

@end

这似乎也没有帮助。


编辑

我已更新此方法以返回:

- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler {

//Creates credentials for logged in user (username/pass)
NSURLCredential *cred = [[AuthController sharedController] userCredentials];

completionHandler(NSURLSessionAuthChallengeUseCredential, cred);

}

仍然什么都不做。

最佳答案

对我来说,您的第一个示例运行良好。我已经使用以下代码进行了测试,没有出现任何问题(这当然是非常不安全的,因为它允许任何服务器证书)。

@implementation SessionTest

- (void) startSession
{
NSURL *url = [NSURL URLWithString:@"https://self-signed.server.url"];

NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: self delegateQueue: [NSOperationQueue mainQueue]];

NSURLSessionDataTask * dataTask = [defaultSession dataTaskWithURL:url
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if(error == nil)
{
NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog(@"Data: %@",text);
}
else
{
NSLog(@"Error: %@", error);
}
}];

[dataTask resume];
}

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler
{
completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
}

@end

更新:这是类接口(interface),SessionTest 类是 NSURLSessionDataDelegate,要开始数据下载,您创建一个 SessionTest 对象并调用 startSession 方法。

@interface SessionTest : NSObject <NSURLSessionDelegate>

- (void) startSession;

@end

关于ios - NSURLSession + 带有自签名证书的服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20230169/

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