iPhone: 安全的RESTful服务器 "此服务器的证书无效"

8

我正在尝试使用安全的RESTful服务,却遇到了错误。

错误 = 错误领域=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];
}

1
我会通过为我的设备设置正确的日期和时间来解决这个问题。 - Hemang
通过将设备的日期和时间设置为自动,解决了相同的问题。 - Tushar J.
3个回答

14

我觉得这可能是由于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

别忘了在发布应用之前将其删除,因为你的应用可能会被拒绝。


出现 #import "NSURLRequest+NSURLRequestSSLY.h" 文件找不到的错误? - Azhar
只需在代码周围加上#ifdef DEBUG和#endif,就可以解决发布到应用商店的问题 ;) - Saren Inden
@VaibhavTekam 如果我想将应用程序提交到商店进行生产,并想要摆脱这个问题,该怎么办? - Jayprakash Dubey
@Jayaprakash Dubey...先把服务器搞对了,问题就不会存在了。 - Vaibhav Tekam

8
您的代码没有问题。您正在使用一个不提供已知证书的HTTPS服务器。如果您自己设置了服务器,则必须购买来自受iOS和大多数其他操作系统信任的大型认证机构的签名证书。
为了开发目的,您可以通过忽略不受信任的证书来测试 REST 服务。按照此指南执行: http://www.cocoanetics.com/2009/11/ignoring-certificate-errors-on-nsurlrequest/ 但是,对于生产使用,我建议您不要使用此方法,因为它将给您的应用程序带来安全漏洞。如果您忽略安全性,也可以使用HTTP而不是HTTPS。

但是这是使用私有API,申请可能会因此被拒绝,你能帮忙使用一些公共API吗?比如我用的那个,但是遇到了问题。 - Azhar
ASIHttpRequest确实有一个简单的BOOL属性,可以禁用证书验证。也许你可以查看项目源代码,或者直接使用ASIHttpRequest而不是NSURLRequest。 - miho

0

我尝试了几种方法,发现这与苹果开发者中心证书有关。 升级您的配置文件,然后再试一次。我已经尝试使用iPad、iPhone 5和5S发布数据,但没有用iPhone 4。 当我更新我的配置文件并安装在我的iPhone 4上时,现在可以正常工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接