iphone:secure restfull server"此服务器的证书无效 [英] iphone: secure restfull server "The certificate for this server is invalid

查看:404
本文介绍了iphone:secure restfull server"此服务器的证书无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用提供错误的安全restful服务

I am trying to consume secure restful service which gives error

错误=错误域= NSURLErrorDomain代码= -1202此服务器的证书无效。您可能连接到假冒xxx.xxx.xxx.xxx的服务器,这可能会使您的机密信息面临风险。

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

working on xCode 4.2, where is the mistake or any step missing.

使用以下代码

RegisterUser。 f

RegisterUser.f

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

RegisterUser.m

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。像你的服务器没有注册或一些。

I feel this might be because of DNS. Something like your server is not registered or some.

尝试将此用于开发目的 -

try using this for development's purpose-

编辑:

创建NSURLRequest + NSURLRequestSSLY.h文件并将这些行添加到其中

Create an "NSURLRequest+NSURLRequestSSLY.h" file and add these lines to it

#import <Foundation/Foundation.h>

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

@end

创建NSURLRequest + NSURLRequestSSLY.m文件,将这些行添加到它

Create an "NSURLRequest+NSURLRequestSSLY.m" file and add these lines to it

#import "NSURLRequest+NSURLRequestSSLY.h"

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

@end

并且不要忘记删除它,因为您的应用可能会被拒绝。

And don't forget to remove it as your app might get rejected.

这篇关于iphone:secure restfull server&quot;此服务器的证书无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆