HTTPS获取请求证书错误xcode

时间:2014-06-10 06:51:45

标签: ios xcode

我真的很困惑,无法解决这个问题。

我想从https Xcode获得回复。 这是网址 - > https://staping.faboo.co.id/(网址不是真实的)

该网址的响应是XML格式,如下所示:

<response>
  <GeneralResponse>
    <status>2</status>
    <desc>Data Not Found</desc>
  </GeneralResponse>
</response>

我的代码fabooViewController.h是这样的:

#import <UIKit/UIKit.h>

@interface FabooViewController : UIViewController

@property (weak, nonatomic) IBOutlet UITextField *passwordField;
@property (weak, nonatomic) IBOutlet UITextField *emailField;
@property (weak, nonatomic) IBOutlet UIButton *buttonLogin;
@property (weak, nonatomic) NSArray *trustedHosts;

@end

我的代码fabooViewController.m是这样的:

- (IBAction)loginAction:(id)sender {
    NSString *email = _emailField.text;
    NSString *password = _passwordField.text;
    NSString *url_string = [NSString stringWithFormat: @"https://staping.faboo.co.id/email=%@&pass=%@",email,password];

    NSLog(@"url : %@",url_string);

    NSURL *URL = [NSURL URLWithString:url_string];

    _trustedHosts = [NSArray arrayWithObjects:@"https://staping.faboo.co.id",nil];

    NSMutableURLRequest *urlRequest=[NSMutableURLRequest requestWithURL:URL];
    NSError *error = nil;
    NSData *data = [ NSURLConnection sendSynchronousRequest: urlRequest returningResponse: nil error: &error ];

    if (error)
    {
        NSLog(@"error %@", [error localizedDescription]);
    }
    else
    {
        NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] ;
        NSLog(@"Result %@", result);
    }
}

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
    return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
    NSLog(@"host : %@",_trustedHosts);
    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];
}

,错误是:

FabooUniversalApp[1570:4903] NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9807)
2014-06-10 13:35:19.541 FabooUniversalApp[1570:60b] error The certificate for this server is invalid. You might be connecting to a server that is pretending to be “staping.faboo.co.id” which could put your confidential information at risk.

我该怎么办?


编辑:

我使用此链接How to use NSURLConnection to connect with SSL for an untrusted cert?作为参考资料

1 个答案:

答案 0 :(得分:0)

<强>解决

在我的文件中。我实现了NSURLRequest

@interface NSURLRequest (DummyInterface)
+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString*)host;
+ (void)setAllowsAnyHTTPSCertificate:(BOOL)allow forHost:(NSString*)host;
@end

我使用此代码从https:

获取结果
NSString *url_string = @"your url";

    NSLog(@"url : %@",url_string);

    NSURL *URL = [NSURL URLWithString:url_string];

    NSMutableURLRequest *urlRequest=[NSMutableURLRequest requestWithURL:URL];
    NSError *error = nil;
    [NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[URL host]];
    NSURLResponse *response;
    NSData *data = [ NSURLConnection sendSynchronousRequest: urlRequest returningResponse:&response error: &error ];

    if (error)
    {
        NSLog(@"error %@", [error localizedDescription]);
    }
    else
    {

        NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] ;
        NSLog(@"Result %@", result);

        NSData *xmlData = [result dataUsingEncoding:NSASCIIStringEncoding];
        NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:xmlData];

        [xmlParser setDelegate:self];

        [xmlParser parse];


    }