NSURLConnection ignore unverified certificate error when sending a synchronise request
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.serverTrust] forAuthenticationChallenge: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];
最新文章
- Sanarus公司的Cassi微创乳房活检设备投入使用
- 51nod比赛
- 3 构建Mysql+heartbeat+DRBD+LVS集群应用系统系列之heartbeat的搭建
- 【POJ3237】Tree 树链剖分+线段树
- 认识Runtime2
- java 14-3 正则表达式的分割
- LeetCode Subsets II (DFS)
- JDBC数据库连接池原理
- Linux kmalloc/kfree 源码解读
- 【BZOJ [1878】[SDOI2009]HH的项链
- 3 委托、匿名函数、lambda表达式
- HTML5 ArrayBuffer:类型化数组 (二)
- UWP:使用Behavior实现FlipView简单缩放效果
- Node.js Web 模块
- webpack + babel
- 【原创】canvas裁剪上传图片异步提交
- 3.Zuul-过滤器
- 【题解】JSOIWC2019 Round1
- LeetCode 49 Group Anagrams(字符串分组)
- label语句、break语句和continue语句