web-services - 如何在 iOS 7 中处理 NS URLAuthenticationChallenge

标签 web-services ssl ios7 nsurlconnection nsurlconnectiondelegate

我想向存在 SSL 问题的 Web 服务发布内容。 我使用了以下方法:

NSURLConnection * urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];

它应该立即开始发送已在请求中设置的数据; 但是服务有安全问题,不能正常工作。但是我想发送数据并想忽略安全问题;所以我使用了以下 NSURLConnectionDelegate 方法:

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

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

  [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}

但它们已被弃用。我如何处理安全问题并告知将数据传递给 Web 服务而不考虑它?

最佳答案

你应该像这样使用 willSendRequestForAuthenticationChallenge。

- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
    self.challenge = challenge;
    [self askUserAcceptSSLError];
}

- (void)askUserAcceptSSLError
{
    // Ask user like UIAlertView or so.
    // Put these responses in UIAlertView delegate ...

    // If User accepts (or force this if you want ignore SSL certificate errors):
    [[self.challenge sender] 
        useCredential:[NSURLCredential credentialForTrust:self.challenge.protectionSpace.serverTrust]
        forAuthenticationChallenge:self.challenge];
    [[self.challenge sender] continueWithoutCredentialForAuthenticationChallenge:self.challenge];

    // If User deny request:
    [[self.challenge sender] cancelAuthenticationChallenge:self.challenge];
}

关于web-services - 如何在 iOS 7 中处理 NS URLAuthenticationChallenge,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22166008/

相关文章:

java - 如何从 EJB 使用 Web 服务

c# - 从 Proxy 类方法调用中查看 SOAP 请求

ssl - 使用 apache camel 连接到 https 服务器

java - 无法启用 SSLv2Hello

java - java服务器和android客户端之间的SSL连接失败

c# - WebRequest 指定 SSL/TLS 密码

iOS 7 navigationController 导航返回错误

iphone - iOS 7 状态栏重叠 UI

objective-c - UIWebView 中的 UIKeyboardAppearance

java - 在有效负载模式下从 Camel 处理器返回的空 SOAP 体