从HTTPS链接提取图像

3

我刚开始学习ios开发,正在尝试从一个使用ssl的网站获取图片,在用笔记本电脑浏览器连接到该网站时,会提示根证书不受信任,虽然我不是该网站的所有者,但我可以完全相信它。

self.eventImage.image = [UIImage imageWithData:
[NSData dataWithContentsOfURL:
[NSURL  URLWithString:imageUrl]]];

我遇到了这个错误:

NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9807)

我尝试通过启动iOS的web浏览器来向用户发送图片链接,当他们这样做时,会收到一条消息询问他们是否信任该链接,如果他们点击“是”,则图片将出现,但是我希望图片能够在应用程序内显示。

我也尝试使用Web View,但是它没有起作用。

大部分类似的问题都建议使用以下方法:

- (BOOL)connection:(NSURLConnection *)
  connection canAuthenticateAgainstProtectionSpace: 
  (NSURLProtectionSpace *)protectionSpace {
    return NO;
    //return [protectionSpace.authenticationMethod isEqualToString:
    //         NSURLAuthenticationMethodServerTrust];
}

- (void)connection:(NSURLConnection *)
  connection didReceiveAuthenticationChallenge:
  (NSURLAuthenticationChallenge *)challenge {
     NSString *imageUri =[self.detailItem objectForKey: @"image"];
     NSArray *trustedHosts = [[NSArray alloc]initWithObjects:imageUri, nil];
     if ([challenge.protectionSpace.authenticationMethod   
         isEqualToString:NSURLAuthenticationMethodServerTrust])
        if ([trustedHosts containsObject:challenge.protectionSpace.host])
               [challenge.sender useCredential:[NSURLCredential credentialForTrust:
                challenge.protectionSpace.serverTrust] forAuthenticationChallenge:
                challenge];

 [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}

但是当我添加它们时,这两种方法从未被调用。

为什么你期望那些connection:...方法被调用?你并没有使用NSURLConnection - rmaddy
正确的答案取决于您是否想要有效地禁用服务器信任评估(就像在您的示例中所做的那样)。如果您真的想要这样做,那么您将无法获得使用安全连接的好处。这绝不是推荐的做法。有关更多信息,请阅读此处:正确覆盖 TLS 链验证 - CouchDeveloper
那么,你是在开发一个只为自己目的的应用吗?还是只是为了积累经验?很好,但无论何种真正的应用程序,现在都必须考虑使用安全连接。但不用担心,我们会提供帮助 :) - CouchDeveloper
4个回答

2
尝试添加这两个方法。
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}

-(void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
[challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}

但是OP没有使用NSURLConnection - rmaddy

1

rmaddy、user2179059和Anindya Sengupta的答案帮助解决了这个问题。

首先,我明确地使用了NSURLConnection,对于安全连接,我使用了这个方法(从这个博客帖子中得到)

-(BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:
(NSURLProtectionSpace *)protectionSpace {
return [protectionSpace.authenticationMethod
        isEqualToString:NSURLAuthenticationMethodServerTrust];
}

-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:
(NSURLAuthenticationChallenge *)challenge {
if ([challenge.protectionSpace.authenticationMethod
      isEqualToString:NSURLAuthenticationMethodServerTrust]) {
      // instead of XXX.XXX.XXX, add the host URL, 
      // if this didn't work, print out the error you receive.
    if ([challenge.protectionSpace.host isEqualToString:@"XXX.XXX.XXX"]) {
        NSLog(@"Allowing bypass...");
        NSURLCredential *credential = [NSURLCredential credentialForTrust:
                                       challenge.protectionSpace.serverTrust];
        [challenge.sender useCredential:credential
             forAuthenticationChallenge:challenge];
    }
 }
[challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}

这与用户2179059不同,它仅限制对该主机的不安全连接。

1
改变你的代码。不要使用NSData dataWithContentsOfURL:,你需要使用自己明确的NSURLConnection。然后你可以利用适当的NSURLConnectionDelegate方法。
另一个选择是使用流行的AFNetworking库

1
真正的问题是如何正确实现委托。使用NSURLConnection委托方法或AFNetworking并不能自动解决这个问题。 - CouchDeveloper
没错。但是在处理步骤2之前,需要先修复步骤1。 - rmaddy
1
我建议第二步应该成为一个单独的问题 ;) - CouchDeveloper

0

我希望你已经在.h文件的接口中包含了NSURLConnectionDelegate协议?

@interface ConnectionExampleViewController : UIViewController <NSURLConnectionDelegate>

这与问题无关。OP甚至没有使用NSURLConnection。代码正在使用NSData dataWithContentsOfURL: - rmaddy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接