简体   繁体   English

如何使用asihttprequest接受自签名证书

[英]How to accept self-signed certificate with asihttprequest

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 : 我现在正在使用ASIHTTPRequest库,如下所示:

- (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. 我已经将setValidatesSecureCertificate设置为YES,希望会发生一些事情,但显然什么都没有,因为我不确定我必须做什么。

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. 任何帮助将不胜感激。

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. 我已经将setValidatesSecureCertificate设置为YES,希望会发生一些事情,但显然什么都没有,因为我不确定我必须做什么。

This is the problem. 这就是问题。 It defaults to YES and you need to set it to NO . 它默认为YES ,您需要将其设置为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. 由于您的证书是自签名的,因此iOS 无法验证证书 - 系统中没有签署证书的受信任机构,因此没有理由说它是有效的。 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. 您必须禁用证书验证才能使自签名证书生效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM