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

NSURLConnection ignore unverified certificate error when sending a synchronise request的更多相关文章

  1. Error when sending message to topic test with key: null, value: 2 bytes with error: (org.apache.kafka.clients.producer.internals.ErrorLoggingCallback)

    windows下使用kafka遇到这个问题: Error when sending message to topic test with key: null, value: 2 bytes with ...

  2. php yii2 使用命令行模式开启脚本 报错 :Error while sending QUERY packet. PID=xxx

    背景:使用Yii2命令行模式开启脚本监控rabbitmq队列(或使用nohup &命令后台监控接口),当队列有订单信息,执行查询,更新操作(相当于PHP文件写个查询,更新,使用命令行启动) 问 ...

  3. code.google.com certificate error: certificate is for www.google.com

    有时候我们会碰到下面错误:code.google.com certificate error: certificate is for www.google.com,类似如下: D:\>go ge ...

  4. Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptabl

    在使用AFNetworking 2.0  的时候本来一切很顺畅,但是中途遇到几个比较坑的地方 这里分享一下爬坑经历,忘读者不能速爬坑! 在发送请求后,NSURLSessionDataTask一直报错 ...

  5. Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/html"

    2015-11-16 10:39:17.235 PullDemo[338:60b] Application windows are expected to have a root view contr ...

  6. How to ignore SSL certificate errors in Apache HttpClient 4.4

    public static CloseableHttpClient acceptsUntrustedCertsHttpClient() throws KeyStoreException, NoSuch ...

  7. AFNetworking3.0出现Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable

    在发送请求后一直报错, 浏览器解析却没有问题, 所以基本可以确定是AFNetworking的问题 下面是解决方法: AFHTTPSessionManager *manager = [AFHTTPSes ...

  8. MVC 本地运行可以发布到IIS 报Sorry, an error occurred while processing your request.解决方案

    发布MVC程序的时候经常遇到这种情况,每次都要搞好久才找到问题.最终找到解决办法: 报500错误大部分的情况还是程序存在异常,但全部被MVC错误拦截成了友好提示, 只要在Web.config下添加一行 ...

  9. Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable con

    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager]; manager.responseSerializer.acceptab ...

随机推荐

  1. C++检测一个文件是否存在

    ifstream::is_open - C++ Reference http://www.cplusplus.com/reference/fstream/ifstream/is_open/ // if ...

  2. LeetCode16 3Sum Closest

    题意: Given an array S of n integers, find three integers in S such that the sum is closest to a given ...

  3. UIImage图片处理,旋转、截取、平铺、缩放等操作

    来源:iOS_小松哥 链接:http://www.jianshu.com/p/9ab1205f5166 有时候我们需要处理图片,比如改变大小,旋转,截取等等,所以今天说一说图片处理相关的一些操作. 本 ...

  4. 解决java中对URL编码的问题

    首先查看javascript中的encodeURI和encodeURLComponent方法的区别. encodeURI:不会对 ASCII 字母和数字进行编码,也不会对这些 ASCII 标点符号进行 ...

  5. IOS 读取本地的Json/plist 文件

    一.一般本地可以存储轻量级数据存储 plist  这个主要是操作字典 方法如下: NSString * sampleFile= [[[NSBundle mainBundle] bundlePath] ...

  6. iOS 画图讲解

    5.画图 (1)画线 //绘图代码写在drawRect里,view加载完成,需要显示的时候调用 //1.获取图形上下文 2.创建路径 3.把图形放入上下文 4.渲染上下文 //drawRect的rec ...

  7. IOS之代理

    作者:王泽豪 时间:2016年4月22日         ★代理是一种设计模式,它的价值在于通过一个统一的模式,解决一个原本并不方便.甚至是几乎不可能解决的问题.         ★代理有两个关注点: ...

  8. Bootstrap基础学习-1

    Bootstrap是一个基于栅格结构的前端结构框架(当然也有JS,JQuery),它的优点是内容框架能够迅速搭建起来,基于媒介查询可以使搭建的页面迅速的适应不同的用户端,无论是手机,平板,还是PC,基 ...

  9. 五.CSS盒子模型

    所谓盒模型,就是浏览器为每个HTML元素生成的矩形盒子.即HTML页面实际上就是由一系列盒子组成.这些盒子是按照可见版式在页面上排布的.并由三个属性进行控制:position属性,display属性, ...

  10. Oracle 学习笔记2:几个入门常用命令

    oracle提供的交互方式有两种:sqlplus(命令行) sqlplusw(图形界面) 进入sqlplus方式:cmd中输入sqlplus 进入sqlplusw方式:cmd中输入sqlplusw 更 ...