我想忽略证书验证,在哪里以及如何使用 XMLRPC Web 服务来做到这一点?
我正在访问 Web 服务并在尝试连接时收到此错误(Web 服务是 XMLRPC,我使用 wordpress xmlrpc 源代码进行请求和处理响应):
Error Domain=NSURLErrorDomain Code=-1202“此服务器的证书无效您可能正在连接到冒充“**.org”的服务器,这可能会使您的机密信息面临风险。”
WebService 人们说忽略证书验证部分,所以如果有人知道如何做到这一点将对我有很大帮助。
经过一些建议后,我使用了下面的 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])
if ([trustedHosts containsObject:challenge.protectionSpace.host])
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
[challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}
I am accessing a web service and getting this error when trying to connect( web service is XMLRPC and I am using wordpress xmlrpc source code for request and handling repsonse):
Error Domain=NSURLErrorDomain Code=-1202 "The certificate for this server is invalid. You might be connecting to a server that is pretending to be “**.org” which could put your confidential information at risk."
WebService people are saying to ignore certificate verification part, so if someone has idea of how to do that will be of great help for me.
after some suggestion I used the below NSURLConnection delegate, stil same error
-(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])
if ([trustedHosts containsObject:challenge.protectionSpace.host])
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
[challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
截至目前,杰伊已经给出了正确的答案。但这两种方法现在已被弃用。
因此,您可以使用该方法:
我已经使用这段代码来克服下面列出的错误:
As of now Jay has given the right answer. But these two methods are deprecated now.
So instead of that you can use that method:
I have used this chunk of code to overcome the below listed error:
正如 aegzorz 指出的,
[NSURLRequest +setAllowsAnyHTTPSCertificate:forHost:]
是私有 API,不应在生产代码中使用。由于它是私有 API,因此肯定会被 App Store 拒绝。处理不受信任证书的已发布方法是使用NSURLConnection
委托方法-connection:canAuthenticateAgainstProtectionSpace:
和-connection:didReceiveAuthenticationChallenge:
。您可以使用这些 API 做很多事情,处理可以想象到的各种身份验证问题。我建议您研究Apple的示例代码 AdvancedURLConnections
As aegzorz noted,
[NSURLRequest +setAllowsAnyHTTPSCertificate:forHost:]
is a private API and shouldn't be used in production code. Since it's a private API, it's a sure means of being rejected from the App Store. The published way to handle untrusted certs is to use theNSURLConnection
delegate method-connection:canAuthenticateAgainstProtectionSpace:
and-connection:didReceiveAuthenticationChallenge:
.There's a lot you can do with these APIs, handling every kind of authentication issue imaginable. I would suggest that you study Apple's sample code AdvancedURLConnections
我正在使用以下内容在正在开发的应用程序中进行测试:
请注意,这是一个私有 API,请勿在生产代码中使用它。
I'm using the following for testing in an app under development:
Note that is a private API, don't use it in production code.
如果您使用
AFNetworking
,则可以使用以下代码:(只是作为临时客户端解决方案!)
If you are using
AFNetworking
, you can use this code:(Just as a temp client-side solution!)