iphone:安全问题的REST服务器“此服务器的证书无效
我试图争取消费RESTful服务这给错误iphone:安全问题的REST服务器“此服务器的证书无效
错误=错误域= NSURLErrorDomain代码= -1202”此服务器的证书无效。您可能会连接到假装为“xxx.xxx.xxx.xxx”的服务器,这可能会使您的机密信息处于危险之中。“
正在使用xCode 4.2,其中是错误还是缺少任何步骤。
使用以下代码
RegisterUser.f
@interface RegisterUser : UIViewController<UITextFieldDelegate,
UIScrollViewDelegate, NSURLConnectionDelegate>
RegisterUser.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
而不要随着您的应用可能被拒绝,您可以将其删除。
失败不在您的代码中。您正在使用不提供已知证书的HTTPS服务器。如果您自己设置了服务器,则必须从iOS和大多数其他操作系统所信任的大型认证机构中购买一份证书。
出于开发目的,您可以通过忽略不可信证书来测试您的REST服务。按照这个指导做到这一点:http://www.cocoanetics.com/2009/11/ignoring-certificate-errors-on-nsurlrequest/
但对于生产用途,我建议你不要使用这种方法,因为它会给你的应用程序带来安全漏洞。如果你忽视安全性,你也可以使用HTTP而不是HTTPS。
我尝试了几种方法,发现它与苹果开发者中心证书有关。 升级您的供应并再次尝试。我曾尝试使用iPad,iPhone 5和5S发布数据,但iPhone 4发布。 当我更新我的配置并在我的iPhone 4上安装时,适用于现在的问题。
我想通过为我的设备设置适当的日期和时间来解决这个问题。 – Hemang
解决了将设备的日期和时间设置为自动的相同问题。 –