分享

NSURLConnection ignore unverified certificate error when sending a synchronise request

 没原创_去搜索 2015-12-22

NSURLConnection ignore unverified certificate error when sending a synchronise request

http://www.cnblogs.com/gnorts/p/3525144.html


Private API, use with caution.

As we all know, it's easy to ignore the unverified certificate error when we are sending an asynchronise request. We can use the NSURLDelegate method to ignore that error, all we need to do is to override the following method:

复制代码

- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge

{

    [challenge.senderuseCredential:[NSURLCredentialcredentialForTrust:challenge.protectionSpace.serverTrustforAuthenticationChallenge:challenge];

}

复制代码

And there's also a way to ignore the unverified certificate error when we are sending a synchronise request:

Before the @implementation of your http client, you could add the following code:

复制代码
@interface NSURLRequest (IgnoreSSL)
+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString*)host;
@end

@implementation NSURLRequest (IgnoreSSL)
+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString*)host
{
    return YES;
}
@end
复制代码

This replacement method gets automatically called and you can decide based on the host to allow any certificates or not. Alternatively you can always return YES regardless of the host parameter to ignore all invalid certificates.

 

Then you would use the following code to get data from the server with an unverified certificate:

NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多