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

  • 相关阅读:
    北京积分落户
    HDU-1054-Strategic Game
    POJ-3020-Antena Placement(最小路径覆盖)
    HDU-4185-Oil Skimming(最大匹配)
    HDU-2389-Rain on your Parade (最大匹配,kopcroft-karp)
    HDU-1083-Courses(最大匹配)
    HDU-1045-Fire Net(最大匹配)
    HDU-2444-The Accomodation of Students(二分图判定,最大匹配)
    Codeforces Round #569 (Div. 2) C. Valeriy and Deque
    Codeforces Round #569 (Div. 2) B. Nick and Array
  • 原文地址:https://www.cnblogs.com/weiboyuan/p/3709084.html
Copyright © 2011-2022 走看看