gpt4 book ai didi

ios - 我们可以忽略 NSMutableURLRequest 中的 ssl 验证吗?

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:00:39 26 4
gpt4 key购买 nike

我是 IOS 开发的新手,我想知道是否可以忽略“NSMutableURLRequest”类的 ssl 验证。如果是这样,请指导我。我通过重写

忽略了通过委托(delegate)进行的 SLL 验证
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace;

我希望在没有委托(delegate)的情况下忽略 SLL 验证。

我的代码片段

 NSString *username = _userName.text;
NSString *password = _password.text;

//HTTP Basic Authentication
NSString *authenticationString = [NSString stringWithFormat:@"%@:%@", username, password];
NSData *authenticationData = [authenticationString dataUsingEncoding:NSUTF8StringEncoding];
NSString *authenticationValue = [authenticationData base64Encoding];

//Set up your request
NSMutableURLRequest *request1 = [[NSMutableURLRequest alloc] initWithURL:url];

// Set your user login credentials
[request1 setValue:[NSString stringWithFormat:@"Basic %@", authenticationValue] forHTTPHeaderField:@"Authorization"];

// Send your request asynchronously
[NSURLConnection sendAsynchronousRequest:request1 queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *responseCode, NSData *responseData, NSError *responseError) {
if ([responseData length] > 0 && responseError == nil){
//logic here
NSString* newStr = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"RESPONSE WITHOUT DELEGATE: %@",newStr);
[[ApplicationStorage applicationStorage] setUserName:_userName.text];
[[ApplicationStorage applicationStorage] setUserName:_password.text];
[[ApplicationStorage applicationStorage] setUserName:_serverAdd.text];
}}];

谢谢

阿米斯

最佳答案

我想这个问题之前已经回答过了。您可以尝试将此添加到您的 AppDelegate.m,在 @end

之后
#if DEBUG

@implementation NSMutableURLRequest (NSURLRequestWithIgnoreSSL)

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

@end

#endif

关于ios - 我们可以忽略 NSMutableURLRequest 中的 ssl 验证吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31075013/

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