關(guān)于ASIHTTPRequest如何信任自簽名證書蕾殴,長話短說,如下幾步:
1黄伊、找到如圖所示的文件
進(jìn)入.m文件,找到
initWithURL:
方法毅舆,如下圖所示西篓,添加setCertificate
(自定義方法,用來導(dǎo)入自簽名證書的)憋活。2岂津、
setCertificate
的具體實(shí)現(xiàn),如下圖:具體代碼如下:
+ (BOOL)extractIdentity:(SecIdentityRef *)outIdentity andTrust:(SecTrustRef*)outTrust fromPKCS12Data:(NSData *)inPKCS12Data
{
OSStatus securityError = errSecSuccess;
NSDictionary *optionsDictionary = [NSDictionary dictionaryWithObject:@"ZJMC@EAM" forKey:(id)kSecImportExportPassphrase];
CFArrayRef items = CFArrayCreate(NULL, 0, 0, NULL);
securityError = SecPKCS12Import((CFDataRef)inPKCS12Data,(CFDictionaryRef)optionsDictionary,&items);
if (securityError == 0) {
CFDictionaryRef myIdentityAndTrust = CFArrayGetValueAtIndex (items, 0);
const void *tempIdentity = NULL;
tempIdentity = CFDictionaryGetValue (myIdentityAndTrust, kSecImportItemIdentity);
*outIdentity = (SecIdentityRef)tempIdentity;
const void *tempTrust = NULL;
tempTrust = CFDictionaryGetValue (myIdentityAndTrust, kSecImportItemTrust);
*outTrust = (SecTrustRef)tempTrust;
} else {
NSLog(@"Failed with error code %d",(int)securityError);
return NO;
}
return YES;
}
- (void)setCertificate {
SecIdentityRef identity = NULL;
SecTrustRef trust = NULL;
NSData *PKCS12Data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"zj-server" ofType:@"p12"]];
[ASIHTTPRequest extractIdentity:&identity andTrust:&trust fromPKCS12Data:PKCS12Data];
[self setClientCertificateIdentity:identity];
}
3悦即、說明
:p12文件是自簽名證書對(duì)應(yīng)的一種格式吮成,這里必須是p12類型的,文件來源于創(chuàng)建自簽名證書的人員辜梳。
其它介紹
:
ASIHTTPRequest詳細(xì)介紹