【问题标题】:iphone: secure restfull server "The certificate for this server is invalidiphone:安全的restfull服务器“此服务器的证书无效
【发布时间】:2012-09-10 07:55:30
【问题描述】:

我正在尝试使用安全的宁静服务,但会出错

Error = Error Domain=NSURLErrorDomain Code=-1202 "此服务器的证书无效。您可能正在连接一个伪装成“xxx.xxx.xxx.xxx”的服务器,这可能会使您的机密信息面临风险。”

在 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];
}

【问题讨论】:

  • 我会通过为我的设备设置正确的日期和时间来解决这个问题。
  • 解决了同样的问题,只需将设备的日期和时间设置为自动。

标签: iphone objective-c ios nsurlconnection nsurlconnectiondelegate


【解决方案1】:

我觉得这可能是因为 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" 文件未找到错误?
  • 只需将#ifdef DEBUG 和#endif 放在它周围,发布到应用商店的问题应该会自行解决;)
  • @VaibhavTekam 如果我想在商店提交应用程序以进行生产并想摆脱这个问题怎么办?
  • @Jayaprakash Dubey ... 让服务器正确。问题根本不存在。
【解决方案2】:

故障不在您的代码中。您正在使用不提供已知证书的 HTTPS 服务器。如果您自己设置了服务器,则必须从受 iOS 和大多数其他操作系统信任的大型证书颁发机构之一购买签名证书。

出于开发目的,您可以通过忽略不受信任的证书来测试您的 REST 服务。请按照本指南进行操作:http://www.cocoanetics.com/2009/11/ignoring-certificate-errors-on-nsurlrequest/

但对于生产用途,我建议您不要使用此方法,因为它会给您的应用程序带来安全漏洞。如果您确实忽略了安全性,您也可以只使用 HTTP 而不是 HTTPS。

【讨论】:

  • 但这是使用私有 API,Apply 可能会因此拒绝申请,您能否提供一些公共 API 的帮助,例如我使用的但有问题解决的问题
  • ASIHttpRequest 确实有一个简单的 BOOL 属性,它会禁用认证验证。也许你看看项目的源代码或者只是使用 ASIHttpRequest 而不是 NSURLRequest。
【解决方案3】:

我尝试了几种方法,发现它与苹果开发者中心证书有关。 升级您的配置并重试。我试过用 iPad、iPhone 5 和 5S 但 iPhone 4 发布数据。 当我在我的 iPhone 4 上更新我的配置和安装后,可以解决现在的问题。

【讨论】:

    猜你喜欢
    • 2017-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-16
    • 1970-01-01
    • 2013-08-09
    相关资源
    最近更新 更多