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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM