zoukankan      html  css  js  c++  java
  • 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];

  • 相关阅读:
    mysql常用的语句示例
    node的模块机制
    mongodb的一些技术点
    php的权限设置流程
    linux常用命令大全
    通过centos7.2搭建个人博客--------------服务器篇
    详解vuex时光机
    详解位运算符的一些特点
    一位资深程序员大牛给予Java初学者的学习路线建议
    JDBC
  • 原文地址:https://www.cnblogs.com/langtianya/p/3726497.html
Copyright © 2011-2022 走看看