0

I've been really confused to solve this problem .

I want get response from https Xcode. this is the URL -> https://staping.faboo.co.id/ ( The url is not real )

Response from that url is XML format like this :

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

My code fabooViewController.h is like this :

#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

My code fabooViewController.m is like this :

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

and the error is:

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.

what should I do?


Edited:

i use this link How to use NSURLConnection to connect with SSL for an untrusted cert? for my reference

Community
  • 1
  • 1
Dirdaa
  • 1
  • 4

1 Answers1

0

SOLVED

in my file.m i implemented NSURLRequest

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

and i use this code to get result from 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];


    }
Dirdaa
  • 1
  • 4