4

Webサービスにアクセスしていて、接続しようとするとこのエラーが発生します(WebサービスはXMLRPCであり、リクエストと応答の処理にwordpress xmlrpcソースコードを使用しています):

エラードメイン=NSURLErrorDomainコード=-1202「このサーバーの証明書が無効です。機密情報を危険にさらす可能性のある「<strong>**。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];
}
4

4 に答える 4

11

今のところ、ジェイは正しい答えを出しています。しかし、これら 2 つの方法は現在非推奨です。

- (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

その代わりに、そのメソッドを使用できます。

-(void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
    if ([[challenge protectionSpace] authenticationMethod] == NSURLAuthenticationMethodServerTrust) {
        [[challenge sender] useCredential:[NSURLCredential credentialForTrust:[[challenge protectionSpace] serverTrust]] forAuthenticationChallenge:challenge];
    }
}

このコードのチャンクを使用して、以下にリストされているエラーを克服しました。

エラー Domain=NSURLErrorDomain Code=-1202 「このサーバーの証明書は無効です。「app.*****.com」になりすましたサーバーに接続している可能性があり、機密情報が危険にさらされる可能性があります。

于 2014-07-20T05:58:39.920 に答える
7

aegzorz が指摘したように、[NSURLRequest +setAllowsAnyHTTPSCertificate:forHost:]はプライベート API であり、運用コードでは使用しないでください。プライベート API ですので、App Store から拒否される確実な手段です。信頼されていない証明書を処理する公開された方法は、NSURLConnectionデリゲート メソッド-connection:canAuthenticateAgainstProtectionSpace:とを使用すること-connection:didReceiveAuthenticationChallenge:です。

これらの API を使用してできることはたくさんあり、考えられるあらゆる種類の認証の問題を処理できます。Apple のサンプル コードAdvancedURLConnectionsを学習することをお勧めします。

于 2010-10-12T19:25:39.593 に答える
4

開発中のアプリでのテストに以下を使用しています。

NSURL* url = // url to webservice
[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];

これはプライベートAPIであることに注意してください。本番コードでは使用しないでください。

于 2010-10-12T16:02:05.473 に答える
1

を使用している場合はAFNetworking、次のコードを使用できます。

(一時的なクライアント側のソリューションとして!)

AFHTTPSessionManager * apiManager = [AFHTTPSessionManager initWithBaseURL:[NSURL URLWithString:baseURL];
AFSecurityPolicy *policy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
policy.allowInvalidCertificates = YES;
apiManager.securityPolicy = policy;
于 2014-04-23T15:42:54.800 に答える