私はObjective-Cに少し慣れていませんが、解決できない問題に遭遇しました。これは主に、ソリューションを正しく実装しているかどうかわからないためです。
自己署名証明書を使用して同期接続を使用してhttpsサイトに接続しようとしています。私は
エラードメイン=NSURLErrorDomainコード=-1202「信頼できないサーバー証明書」
このフォーラムでいくつかの解決策を見たというエラー。私が見つけた解決策は、次を追加することでした。
- (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];
}
NSURLDelegateに送信して、すべての証明書を受け入れます。次のコマンドだけを使用してサイトに接続すると、次のようになります。
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://examplesite.com/"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
それはうまく機能し、チャレンジが受け入れられているのがわかります。ただし、同期接続を使用して接続しようとすると、エラーが発生し、ログに記録したときにチャレンジ関数が呼び出されません。
チャレンジメソッドを使用するために同期接続を取得するにはどうすればよいですか?それは、URLConnectionのdelegate:self部分と関係がありますか?また、NSURLDelegate内でデータを送受信するためのログがあります。これは、接続関数によって呼び出されますが、同期関数によっては呼び出されません。
同期部分に使用しているもの:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: [NSURL URLWithString:@"https://examplesite.com/"]];
[request setHTTPMethod: @"POST"];
[request setHTTPBody: [[NSString stringWithString:@"username=mike"] dataUsingEncoding: NSUTF8StringEncoding]];
dataReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(@"%@", error);
stringReply = [[NSString alloc] initWithData:dataReply encoding:NSUTF8StringEncoding];
NSLog(@"%@", stringReply);
[stringReply release];
NSLog(@"Done");
私が言ったように、私はObjective Cに少し慣れていないので、親切にしてください:)
助けてくれてありがとう。マイク