我正在尝试使用HttpClient在Android手机上建立Https连接。由于证书是自签名的,我一直得到“javax.net.ssl.SSLException:不可信的服务器证书”。
请帮帮我。 以下是代码和日志。
public class CustomClientSSLActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
DefaultHttpClient client = new DefaultHttpClient();
SchemeRegistry registry = new SchemeRegistry();
SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
registry.register(new Scheme("https", socketFactory, 443));
SingleClientConnManager mgr = new SingleClientConnManager(client.getParams(), registry);
DefaultHttpClient httpClient = new DefaultHttpClient(mgr, client.getParams());
// Set verifier
HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
// Example send http request
final String url = "https://192.168.1.140";
HttpPost httpPost = new HttpPost(url);
HttpResponse response=null;
try {
response = httpClient.execute(httpPost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("--response "+response);
}
}
LOG ---
W/System.err( 868): javax.net.ssl.SSLException: Not trusted server certificate
W/System.err( 868): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:360)
W/System.err( 868): at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:92)
W/System.err( 868): at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:321)
W/System.err( 868): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:129)
W/System.err( 868): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
W/System.err( 868): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
W/System.err( 868): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:348)
W/System.err( 868): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
W/System.err( 868): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
W/System.err( 868): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
W/System.err( 868): at com.qteq.customclientssl.CustomClientSSLActivity.onCreate(CustomClientSSLActivity.java:46)
W/System.err( 868): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
W/System.err( 868): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
W/System.err( 868): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
W/System.err( 868): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
W/System.err( 868): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
W/System.err( 868): at android.os.Handler.dispatchMessage(Handler.java:99)
W/System.err( 868): at android.os.Looper.loop(Looper.java:123)
W/System.err( 868): at android.app.ActivityThread.main(ActivityThread.java:4363)
W/System.err( 868): at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err( 868): at java.lang.reflect.Method.invoke(Method.java:521)
W/System.err( 868): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
W/System.err( 868): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
W/System.err( 868): at dalvik.system.NativeStart.main(Native Method)
W/System.err( 868): Caused by: java.security.cert.CertificateException: java.security.cert.CertPathValidatorException: TrustAnchor for CertPat
W/System.err( 868): at org.apache.harmony.xnet.provider.jsse.TrustManagerImpl.checkServerTrusted(TrustManagerImpl.java:168)
W/System.err( 868): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:355)
W/System.err( 868): ... 23 more
W/System.err( 868): Caused by: java.security.cert.CertPathValidatorException: TrustAnchor for CertPath not found.
W/System.err( 868): at org.bouncycastle.jce.provider.PKIXCertPathValidatorSpi.engineValidate(PKIXCertPathValidatorSpi.java:149)
W/System.err( 868): at java.security.cert.CertPathValidator.validate(CertPathValidator.java:211)
W/System.err( 868): at org.apache.harmony.xnet.provider.jsse.TrustManagerImpl.checkServerTrusted(TrustManagerImpl.java:164)
W/System.err( 868): ... 24 more
I/System.out( 868): --response null
I/ActivityManager( 52): Displayed activity com.qteq.customclientssl/.CustomClientSSLActivity: 1634 ms (total 1634 ms)
答案 0 :(得分:0)
看看这两篇文章,我想他们会帮助你:)。
Trusting all certificates using HttpClient over HTTPS
在这篇博客中你也有一个例子:http://blog.crazybob.org/2010/02/android-trusting-ssl-certificates.html
希望这会有所帮助......