gpt4 book ai didi

iphone:安全的 restfull 服务器“此服务器的证书无效

转载 作者:可可西里 更新时间:2023-11-01 03:36:26 29 4
gpt4 key购买 nike

我正在尝试使用会出错的安全 Restful 服务

Error = Error Domain=NSURLErrorDomain Code=-1202 “此服务器的证书无效。您可能正在连接到一个伪装成“xxx.xxx.xxx.xxx”的服务器,这可能会使您的 secret 信息处于危险之中。”

在 xCode 4.2 上工作,哪里有错误或缺少任何步骤。

使用下面的代码

注册用户.f

@interface RegisterUser : UIViewController<UITextFieldDelegate,
UIScrollViewDelegate, NSURLConnectionDelegate>

注册用户.m

- (IBAction)SubmitBtnAction:(id)sender {

NSURL *url = [NSURL URLWithString:@"https://xx.xx.xx.xxx:8223/jaxrs/tunedoorgateway/getCountries"];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];

[NSURLConnection sendAsynchronousRequest:urlRequest queue:[[NSOperationQueue alloc] init]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{

if ([data length] >0 && error == nil)
{
NSLog(@"Data = %@", data);
// DO YOUR WORK HERE

}
else if ([data length] == 0 && error == nil)
{
NSLog(@"Nothing was downloaded.");
}
else if (error != nil){
NSLog(@"Error = %@", error);
}

}];

}


- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
NSLog(@"This is canAuthenticateAgainstProtectionSpace");
return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}


- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
// if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
// if ([trustedHosts containsObject:challenge.protectionSpace.host])
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];

[challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
NSLog(@"This is didReceiveAuthenticationChallenge");
// [[challenge sender] cancelAuthenticationChallenge:challenge];
}

最佳答案

我觉得这可能是因为 DNS,比如你的服务器没有注册。

尝试使用它进行开发:

创建一个 NSURLRequest+NSURLRequestSSLY.h 文件并向其中添加这些行

#import <Foundation/Foundation.h>

@interface NSURLRequest (NSURLRequestSSLY)
+(BOOL)allowsAnyHTTPSCertificateForHost:(NSString*)host;

@end

创建一个 NSURLRequest+NSURLRequestSSLY.m 文件并添加这些行

#import "NSURLRequest+NSURLRequestSSLY.h"

@implementation NSURLRequest (NSURLRequestSSLY)
+(BOOL)allowsAnyHTTPSCertificateForHost:(NSString*)host
{
return YES;
}

@end

并且不要忘记在发布之前将其删除,因为您的应用可能会被拒绝。

关于iphone:安全的 restfull 服务器“此服务器的证书无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12347410/

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