接受自签名SSL证书

时间:2013-10-06 13:09:12

标签: java android http ssl

我正在使用以下方法在Android中发布HTTP帖子:

public static Strign post(String url, List<BasicNameValuePair> postParams,
        HttpClient client) throws ClientProtocolException, IOException {
    if (client == null) {
        client = getClient();
    }
    HttpPost httppost = new HttpPost(url);

    if ((postParams == null)) {
        postParams = new ArrayList<BasicNameValuePair>();
    }
    httppost.setEntity(new UrlEncodedFormEntity(postParams, "UTF-8"));
    // Execute HTTP Post Request
    HttpResponse response = client.execute(httppost);
    return requestToString(response);
}

但是当我使用SSL连接和自签名证书时,它会引发以下异常:

Catch exception while startHandshake: javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
return an invalid session with invalid cipher suite of SSL_NULL_WITH_NULL_NULL
javax.net.ssl.SSLPeerUnverifiedException: No peer certificate
    at org.apache.harmony.xnet.provider.jsse.SSLSessionImpl.getPeerCertificates(SSLSessionImpl.java:137)
    at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:93)
    at org.apache.http.conn.ssl.SSLSocketFactory.createSocket(SSLSocketFactory.java:381)
    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:165)
    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:367)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:748)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:519)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:497)

如何解决此问题并接受自签名证书?

由于

0 个答案:

没有答案