0

我正在使用以下方法在 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)

我该如何解决这个问题并接受自签名证书?

谢谢

4

0 回答 0