首页 > 其他 > 详细

NSURLConnection ignore unverified certificate error when sending a synchronise request

时间:2014-01-18 18:45:16      阅读:507      评论:0      收藏:0      [点我收藏+]

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:

bubuko.com,布布扣

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

{

    [challenge.senderuseCredential:[NSURLCredentialcredentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];

}

bubuko.com,布布扣

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:

bubuko.com,布布扣
@interface NSURLRequest (IgnoreSSL)
+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString*)host;
@end

@implementation NSURLRequest (IgnoreSSL)
+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString*)host
{
    return YES;
}
@end
bubuko.com,布布扣

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

NSURLConnection ignore unverified certificate error when sending a synchronise request

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!