How to accept self-signed certificate with asihttp

2019-03-26 14:36发布

I am trying to get a self-signed certificate to work with my application.

I am using the ASIHTTPRequest library at the moment like so :

- (IBAction)sendHttpsRequest
{    
    //Set request address
    NSMutableString *databaseURL = [[NSMutableString alloc] initWithString:@"https://142.18.87.46:443"];

    //call ASIHTTP delegates (Used to connect to database)
    NSURL *url = [NSURL URLWithString:databaseURL];

    //This sets up all other request
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];

    [request setDelegate:self];
    [request setValidatesSecureCertificate:YES];
    [request startAsynchronous];
}

I have set setValidatesSecureCertificate to YES in the hope that something would happen but obviously nothing has because I'm not sure what I have to do.

This is the error I'm getting in my log

2011-12-06 14:27:33.514 connectionTest[916:207] Error Domain=ASIHTTPRequestErrorDomain Code=1 "A connection failure occurred: SSL problem (Possible causes may include a bad/expired/self-signed certificate, clock set to wrong date)" UserInfo=0x683a860 {NSUnderlyingError=0x68390d0 "The operation couldn’t be completed. (OSStatus error -9807.)", NSLocalizedDescription=A connection failure occurred: SSL problem (Possible causes may include a bad/expired/self-signed certificate, clock set to wrong date)}

Any help would be greatly appreciated.

1条回答
一纸荒年 Trace。
2楼-- · 2019-03-26 14:45

I have set setValidatesSecureCertificate to YES in the hope that something would happen but obviously nothing has because I'm not sure what I have to do.

This is the problem. It defaults to YES and you need to set it to NO. As your certificate is self-signed, iOS can't validate the certificate - there is no trusted authority in the system that has signed the certificate, so it has no basis for saying that it is valid. So if you ask it to validate the certificate (which is the default), it has to reject it. You have to disable certificate validation to get self-signed certificates to work.

查看更多
登录 后发表回答