klausa2
Erfahrenes Mitglied
- 36
Hallo,
mein Problem: ich möchte mittels HttpClient und POST per https, also mit SSL, auf einen Server zugreigen, der ein selbst igniertes Certificate verwendet. Der HttpClient wirft da immer eine SSLException
javax.net.ssl.SSLException: Not trusted server certificate
Nun habe ich im Web gesucht und folgenden Code gefunden, der sich nur mit
"import org.apache.http.conn.ssl.SSLSocketFactory"
aber nicht mit
"import javax.net.ssl.SSLSocketFactory"
compilieren lässt, da in javax die methode "getHostnameVerifier() " nicht vorhanden ist.:
mein Problem: ich möchte mittels HttpClient und POST per https, also mit SSL, auf einen Server zugreigen, der ein selbst igniertes Certificate verwendet. Der HttpClient wirft da immer eine SSLException
javax.net.ssl.SSLException: Not trusted server certificate
Nun habe ich im Web gesucht und folgenden Code gefunden, der sich nur mit
"import org.apache.http.conn.ssl.SSLSocketFactory"
aber nicht mit
"import javax.net.ssl.SSLSocketFactory"
compilieren lässt, da in javax die methode "getHostnameVerifier() " nicht vorhanden ist.:
Code:
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] DefaultHttpClient getTolerantClient() { [/SIZE]
[SIZE=2]DefaultHttpClient client = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] DefaultHttpClient(); [/SIZE]
[SIZE=2]SSLSocketFactory sslSocketFactory = (SSLSocketFactory) client [/SIZE]
[SIZE=2].getConnectionManager().getSchemeRegistry().getScheme([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"https"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]) [/SIZE]
[SIZE=2].getSocketFactory(); [/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]final[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] X509HostnameVerifier delegate = sslSocketFactory.getHostnameVerifier(); [/SIZE]
[SIZE=2]AbstractVerifier av = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] AbstractVerifier() { [/SIZE][/LEFT]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] verify(String host, String[] cns, String[] subjectAlts) [/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]throws[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] SSLException { [/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]boolean[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ok = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]true[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]; // true is default in this case[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]try[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] { [/SIZE]
[SIZE=2]delegate.verify(host, cns, subjectAlts); [/SIZE]
[SIZE=2]} [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]catch[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] (SSLException e) { [/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]for[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] (String cn : cns) { [/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]if[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] (cn.startsWith([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"*."[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])) { [/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]try[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] { [/SIZE]
[SIZE=2]delegate.verify(host, [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] String[] { cn.substring(2) }, subjectAlts); [/SIZE]
[SIZE=2]ok = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]true[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]; [/SIZE]
[SIZE=2]} [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]catch[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] (Exception e1) { } [/SIZE]
[SIZE=2]} [/SIZE]
[SIZE=2]} [/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]if[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2](!ok) [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]throw[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] e; [/SIZE]
[SIZE=2]} [/SIZE]
[SIZE=2]} [/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// end verify [/COLOR][/SIZE][/COLOR][/SIZE][/LEFT]
[SIZE=2]}; [/SIZE]
[LEFT][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// end [U]abstractverifier[/U][/COLOR][/SIZE][/COLOR][/SIZE][/LEFT]
[LEFT][SIZE=2]sslSocketFactory.setHostnameVerifier(av); [/SIZE]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]return[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] [U]client[/U]; [/SIZE]
[SIZE=2]} [/SIZE][/LEFT]
[/LEFT]
Klappt auf dem Desktop prima, aber unter Anfroid nicht. Ein Breakpunt am Anfang von verify wird nie erreicht.
Frage 1: Hat jemand ne Idee, warum das nicht läuft?
Frage 2, wichtiger: Wie kann ich meinen HttpClient dazu bringen, selbst signierte Zertificate zu akzeptieren?? Oder wie kann ich als root user dem System keystore das Zertifikat zufügen (wäre mir weniger angenehm, da es dann für alle Apps gelten würde).
Frage 2, wichtiger: Wie kann ich meinen HttpClient dazu bringen, selbst signierte Zertificate zu akzeptieren?? Oder wie kann ich als root user dem System keystore das Zertifikat zufügen (wäre mir weniger angenehm, da es dann für alle Apps gelten würde).