https在android上发布

时间:2011-04-05 03:49:39

标签: java android http https http-post

我需要https发布到数据库服务器。我一直在寻找SO和谷歌,并找到了一些关于这个问题的例子和帖子,但我仍然无法理解人们发布的解决方案。有人可以分析我有什么,并告诉我我需要改变什么来让这个工作?我接受所有证书,我只需要最快捷,最简单的方法来解决这个问题。我正处于截止日期,这是我工作的瓶颈。我得到的错误与代码一起发布。

我在下面发布的代码是帮助我所有http发布的助手类的一部分。

如果您需要更多信息,请提前告知我们!谢谢,谢谢!

public static synchronized int authenticate(String uName, String pWord) {
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("action", "authentication"));
    nameValuePairs.add(new BasicNameValuePair("username", "uName"));
    nameValuePairs.add(new BasicNameValuePair("password", "pWord"));

    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("https://ipaddress/link/admin.php");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        String responseBody = EntityUtils.toString(response.getEntity());
        Log.v("smartdbhelper authenticate", responseBody);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return 0;
}   

04-05 03:47:19.415: WARN/System.err(279): javax.net.ssl.SSLException: Not trusted server certificate
04-05 03:47:19.437: WARN/System.err(279):     at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:371)
04-05 03:47:19.446: WARN/System.err(279):     at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:92)
04-05 03:47:19.446: WARN/System.err(279):     at org.apache.http.conn.ssl.SSLSocketFactory.createSocket(SSLSocketFactory.java:381)
04-05 03:47:19.476: WARN/System.err(279):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:164)
04-05 03:47:19.476: WARN/System.err(279):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
04-05 03:47:19.486: WARN/System.err(279):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
04-05 03:47:19.486: WARN/System.err(279):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:348)
04-05 03:47:19.495: WARN/System.err(279):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
04-05 03:47:19.506: WARN/System.err(279):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
04-05 03:47:19.506: WARN/System.err(279):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
04-05 03:47:19.516: WARN/System.err(279):     at cpe495.smartapp.SmartDBHelper.authenticate(SmartDBHelper.java:50)
04-05 03:47:19.525: WARN/System.err(279):     at cpe495.smartapp.DataSender.submitData(DataSender.java:22)
04-05 03:47:19.525: WARN/System.err(279):     at cpe495.smartapp.DataSender.sendData(DataSender.java:17)
04-05 03:47:19.525: WARN/System.err(279):     at cpe495.smartapp.SmartApp$1.dataReceivedReceived(SmartApp.java:48)
04-05 03:47:19.536: WARN/System.err(279):     at cpe495.smartapp.ConnectDevice.fireDataReceivedEvent(ConnectDevice.java:79)
04-05 03:47:19.536: WARN/System.err(279):     at cpe495.smartapp.ConnectDevice.run(ConnectDevice.java:46)
04-05 03:47:19.536: WARN/System.err(279):     at java.lang.Thread.run(Thread.java:1096)
04-05 03:47:19.547: WARN/System.err(279): Caused by: java.security.cert.CertificateException: java.security.cert.CertPathValidatorException: TrustAnchor for CertPath not found.
04-05 03:47:19.566: WARN/System.err(279):     at org.apache.harmony.xnet.provider.jsse.TrustManagerImpl.checkServerTrusted(TrustManagerImpl.java:168)
04-05 03:47:19.566: WARN/System.err(279):     at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:366)
04-05 03:47:19.576: WARN/System.err(279):     ... 16 more
04-05 03:47:19.576: WARN/System.err(279): Caused by: java.security.cert.CertPathValidatorException: TrustAnchor for CertPath not found.
04-05 03:47:19.616: WARN/System.err(279):     at org.bouncycastle.jce.provider.PKIXCertPathValidatorSpi.engineValidate(PKIXCertPathValidatorSpi.java:149)
04-05 03:47:19.616: WARN/System.err(279):     at java.security.cert.CertPathValidator.validate(CertPathValidator.java:202)
04-05 03:47:19.626: WARN/System.err(279):     at org.apache.harmony.xnet.provider.jsse.TrustManagerImpl.checkServerTrusted(TrustManagerImpl.java:164)
04-05 03:47:19.626: WARN/System.err(279):     ... 17 more

2 个答案:

答案 0 :(得分:1)

我刚从这个链接完成了这个概念:

http://blog.crazybob.org/2010/02/android-trusting-ssl-certificates.html

这对我来说很好。有点比我更长的解决方案,但我没有找到使用HttpClient的任何其他方式。另请查看此问题以获取更多想法:

Https Connection Android

答案 1 :(得分:0)

您似乎需要告诉客户which certificate authority (CA) certificate to use when validating the server's certificate

如果您对相关服务器拥有管理控制权,则可以选择已在Android上“受信任”列表中的证书颁发机构(至少,我认为Android预装了几十个,同样如此大多数Web浏览器),或者您可以通过TrustAnchor在您的控制下添加一个特定于您的应用程序的CA. (这可能是更便宜的方法,但如果您希望人们在应用程序之外使用您的网站,您可能只想花100美元/年的证书。)