打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
NSURLConnection ignore unverified certificate error when sending a synchronise request

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

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
INFO: WinInet 错误代码 (12001 到 12156)
FastCGI Error
ios发送http GET和POST请求
ERROR: SSL verification error
lftp出现“Fatal Error: Certificate Verification: Not Trusted”问题解决
ERROR: The requested URL could not be retrieved
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服