gpt4 book ai didi

java - 在 Android 上发布 https

转载 作者:行者123 更新时间:2023-12-01 15:53:25 24 4
gpt4 key购买 nike

我需要 https post 到数据库服务器。我一直在浏览 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

最佳答案

我刚刚通过此链接完成了这个概念的实现:

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

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

Https Connection Android

关于java - 在 Android 上发布 https,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5546975/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com