,如果你有一个非自签名的证书或将您加入这显然只工作:
的#define _AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_你的PCH文件。如果您使用的可可豆荚为此,您可能需要继承AFHTTPRequestOperation和实施:
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
if ([[protectionSpace authenticationMethod] isEqualToString:NSURLAuthenticationMethodServerTrust]) {
if ([self bypassSslCertValidation:protectionSpace])
return YES;
else
return [super connection:connection canAuthenticateAgainstProtectionSpace:protectionSpace];
}
return [super connection:connection canAuthenticateAgainstProtectionSpace:protectionSpace];
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
if ([self bypassSslCertValidation:challenge.protectionSpace]) {
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
return;
}
else
return [super connection:connection didReceiveAuthenticationChallenge:challenge];
return;
}
}
- (BOOL) bypassSslCertValidation:(NSURLProtectionSpace *) protectionSpace
{
if (ENVIRONMENT_TYPE == DEV_ENV || ENVIRONMENT_TYPE == STAGING_ENV) {
return YES;
}
return NO;
}
然后告诉AFNEtworking使用新的子类:
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@""]];
[client registerHTTPOperationClass:[YourSubClassHTTPRequestOperation class]];
这不是在做最简单的事情世界和技术上忽视自签名没有使其工作,但如果您使用标准SLL证书它很可能会工作得很好,请记住删除此代码或仅在调试时才可用,如果您计划发布。
添加到答案,因为评论有字符限制!
几个选择看,可以手动添加到队列中的headers
返回操作:
- (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)urlRequest
或者在您的自定义子类的操作传递到这一个:
- (void)enqueueHTTPRequestOperation:(AFHTTPRequestOperation *)operation;
报道在日志什么错误?另外,你的服务器证书是否有效? – nioq 2013-02-15 18:23:50