AKG*_*AKG 4 iphone xml-rpc simplexmlrpcserver
我正在访问一个Web服务并在尝试连接时收到此错误(Web服务是XMLRPC,我使用wordpress xmlrpc源代码请求和处理repsonse):
错误域= NSURLErrorDomain代码= -1202"此服务器的证书无效.您可能正在连接到假装为" ** .org" 的服务器,这可能会使您的机密信息面临风险."
WebService的人说要忽略证书验证部分,所以如果有人知道怎么做,那将对我有很大的帮助.
在一些建议之后我使用了下面的NSURLConnection委托,stil同样的错误
-(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];
}
Run Code Online (Sandbox Code Playgroud)
Har*_*ngh 11
截至目前,周杰伦给出了正确的答案.但是这两种方法现在已经弃用了.
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace // deprecated over iOS 5.0. Not even called in iOS 7.0
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge // deprecated over iOS 5.0. Not even called in iOS 7.0
Run Code Online (Sandbox Code Playgroud)
因此,您可以使用该方法:
-(void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
if ([[challenge protectionSpace] authenticationMethod] == NSURLAuthenticationMethodServerTrust) {
[[challenge sender] useCredential:[NSURLCredential credentialForTrust:[[challenge protectionSpace] serverTrust]] forAuthenticationChallenge:challenge];
}
}
Run Code Online (Sandbox Code Playgroud)
我使用这段代码来克服下面列出的错误:
错误域= NSURLErrorDomain代码= -1202"此服务器的证书无效.您可能正在连接到假装为"app.*****.com"的服务器,这可能会使您的机密信息面临风险.
正如aegzorz所说,它[NSURLRequest +setAllowsAnyHTTPSCertificate:forHost:]是一个私有API,不应该用在生产代码中.由于它是一个私有API,因此它肯定会被App Store拒绝.处理不受信任证书的已发布方法是使用NSURLConnection委托方法-connection:canAuthenticateAgainstProtectionSpace:和-connection:didReceiveAuthenticationChallenge:.
您可以使用这些API做很多事情,处理可以想象的各种身份验证问题.我建议您学习Apple的示例代码AdvancedURLConnections
| 归档时间: |
|
| 查看次数: |
16302 次 |
| 最近记录: |