iOS ASIHTTPRequest用https协议加密请求
iOS 终端请求服务端数据时,为了保证数据安全,我们一般会使用https协议加密,而对于iOS的网络编程,我们一般会使用开源框架:ASIHTTPRequest,但是如果使用传统的http方式,即使忽略验证的话,程序也会报[error-9844]的错误,具体错误如下描述:
【Error Domain=ASIHTTPRequestErrorDomain Code=1 "A connection failure occurred" UserInfo=0x6aafa30 {NSUnderlyingError=0x6a3fd90 "The operation couldn’t be completed. (OSStatus error -9844.)", NSLocalizedDescription=A connection failure occurred}】
那么该如何解决该问题呢?经过查找解决方案如下:
解决方案1
1) [request setValidatesSecureCertificate:NO];
2) 在ASIHTTPRequest.m文件中查找”https”,向sslProperties字典中增加kCFStreamSSLLevel对象,网上的参考方法:
[sslProperties setObject:(NSString *)(CFStringRef*)kCFStreamSocketSecurityLevelSSLv3 forKey:(NSString*)kCFStreamSSLLevel];
但是,sslProperties字典并非可变字典,所以你需要将sslProperties修改为可变字典NSMutableDictionary。
解决方案2
如果需要更加灵活的对SSL security level进行设置,可以将CFStringRef*sslSecurityLevel作为变量提到ASIHTTPRequest.h文件的property中,具体操作如下:
1) 在ASIHTTPRequest.h的文件中添加声明:
CFStringRef *sslSecurityLevel;
2) 在ASIHTTPRequest.h的文件中添加CFStringRef属性:
@property (assign) CFStringRef *sslSecurityLevel;
3) 在ASIHTTPRequest.m的文件中添加CFStringRef属性:
@synthesize sslSecurityLevel;
4) 在ASIHTTPRequest.m的文件修改:
NSDictionary *sslProperties = [[NSDictionary alloc]initWithObjectsAndKeys:??,将不可变字典更改为可变字典(NSMutableDictionary),即:
NSMutableDictionary *sslProperties = [[NSMutableDictionary alloc]initWithObjectsAndKeys:??
5) 在4)修改语句结束的地方添加设置SSL security level的代码:
// Use requested SSL security level
if ([self sslSecurityLevel] != nil) {
[sslProperties setObject:(NSString *)[self sslSecurityLevel] forKey:(NSString *)kCFStreamSSLLevel];
}
6) 在调用请求的request中做如下设置:
[request setValidatesSecureCertificate:NO];
[request setSslSecurityLevel:(CFStringRef*)kCFStreamSocketSecurityLevelSSLv3];
相关文章
- 深度学习之Matlab 转C++在iOS上測试CNN手型识别
- iOS app 企业内部发布及HTTPS服务器配置
- https://github.com/coolnameismy/BabyBluetooth github上的一个ios 蓝牙4.0的库并带文档和教程
- iOS 可高度自定义的底部弹框
- iOS 移动端生成工具开发
- https的证书认证 iOS版
- iOS适配HTTPS,创建一个自签名的SSL证书(x509)具体步骤
- iOS UIWebView 访问https绕过证书验证的方法
- iOS布局进化史
- ios中的coredata的使用
- uni-app如何判断是否安装腾讯QQ微信微博支付宝淘宝等客户端,解决IOS应用审核被拒:用户应该能够使用微信登录并访问自己的帐户,而无需安装任何其他应用程序。
- ios开源项目(各种有用的第三方库)
- iOS为移动中的UIView(UIButton )添加点击事件
- 【iOS发展-89】UIGestureRecognizer完整的旋转手势识别、缩放和拖拽等效果
- ios 获取button所在的cell对象, 注意:ios7 =< System Version < ios8 获取cell对象的差别
- iOS开发 - 保存图片到相册和保存屏幕截图到相册
- iOS - HTTPS接口加密和身份认证