8

I am trying to consume secure restful service which gives error

Error = Error Domain=NSURLErrorDomain Code=-1202 "The certificate for this server is invalid. You might be connecting to a server that is pretending to be “xxx.xxx.xxx.xxx” which could put your confidential information at risk."

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

using following code

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

3 に答える 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

また、アプリが拒否される可能性があるため、公開する前に削除することを忘れないでください。

于 2012-09-10T08:08:23.597 に答える
8

失敗はあなたのコードにはありません。既知の証明書を提供しないHTTPSサーバーを使用しています。自分でサーバーをセットアップした場合は、iOS やその他のほとんどのオペレーティング システムで信頼されている大きな認証機関の 1 つから署名済みの証明書を購入する必要があります。

開発目的で、信頼されていない証明書を無視して REST サービスをテストできます。それを行うには、このガイドに従ってください: http://www.cocoanetics.com/2009/11/ignoring-certificate-errors-on-nsurlrequest/

ただし、本番環境で使用する場合は、アプリケーションにセキュリティ リークが発生するため、この方法を使用しないことをお勧めします。セキュリティを無視する場合は、HTTPS の代わりに HTTP を使用することもできます。

于 2012-09-10T08:07:09.020 に答える
0

いくつかの方法を試してみたところ、Apple Developer Center の証明書に関連していることがわかりました。プロビジョニングをアップグレードして、もう一度お試しください。iPad、iPhone 5 および 5S でデータを投稿しようとしましたが、iPhone 4 です。プロビジョニングを更新して iPhone 4 にインストールすると、問題なく動作します。

于 2014-08-27T19:09:27.190 に答える