ios下https代理访问证书问题

在ios下,需要打开https链接,但是如果其中使用了代理访问,则会被默认返回证书验证错误,无法正常访问

通常是在国内访问国外facebook的情况下

这是因为

https访问的时候,会验证一次证书,如果用了代理,证书验证的时候会被认为有风险,则会拒绝掉连接

也就是为了避免中间人攻击而做的限制

这里可以考虑先用NSURLConnection创建一个https连接,让本次针对目标地址的连接在验证时忽略证书,就可以保证之后的连接再也没证书验证问题了

           NSString* strUrl = [NSString stringWithFormat:@https://graph.facebook.com/me];
           NSURLRequest* reqUrl = [NSURLRequest requestWithURL:[NSURL URLWithString:strUrl]];
           NSURLConnection* conn = [NSURLConnection connectionWithRequest:reqUrl delegate:self];            
           [conn start];

 


- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"connection didFailWithError result: %@", error);
}
- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection
{
    return NO;
}


- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{

   //第一次验证通过,之后取消验证

    if ([challenge previousFailureCount] ==0)
        
    {
        NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]; 
[[challenge sender] useCredential:credential forAuthenticationChallenge:challenge]; }
else { [[challenge sender] cancelAuthenticationChallenge:challenge]; } } // Deprecated authentication delegates. - (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace { return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]; } - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { //第一次验证通过,之后取消验证 if ([challenge previousFailureCount] ==0) { NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
[[challenge sender] useCredential:credential forAuthenticationChallenge:challenge]; }
else { [[challenge sender] cancelAuthenticationChallenge:challenge]; } } - (void)connection:(NSURLConnection *)connection didCancelAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { } - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { //做你需要做的事情 }

不过这个最好看看最新的sdk支不支持了。。

转载于:https://www.cnblogs.com/flamefox/p/4241475.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值