gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:04:03 33 4
gpt4 key购买 nike

我想向存在 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/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com