javax.net.ssl.sslpeerunverifiedexception 无对等证书 [英] javax.net.ssl.sslpeerunverifiedexception no peer certificate
问题描述
我正在尝试通过将数据从 Android 应用程序发布到 PHP 服务器来将记录插入到 MySQL 中.我已将 INTERNET 权限添加到 AndroidManifest.xml
I am trying to insert a record into MySQL by posting data to a PHP server from an Android app. I have added the INTERNET permission to AndroidManifest.xml
我收到 javax.net.ssl.SSLPeerUnverifiedException: No peer certificate
安卓代码
private void senddata(ArrayList<NameValuePair> data)
{
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("https://10.0.2.2/insert222.php");
httppost.setEntity(new UrlEncodedFormEntity(data));
HttpResponse response = httpclient.execute(httppost);
}
catch (Exception e) {
// TODO: handle exception
Log.e("log_tag", "Error: "+e.toString());
}
}
有人可以帮忙吗?
推荐答案
警告:不要在您不完全信任的网络上使用的生产代码中实现这一点.尤其是通过公共互联网进行的任何事情.此链接给出了更正确的答案.这里是使用 SSL 的实现.
Warning: Do not implement this in production code you are ever going to use on a network you do not entirely trust. Especially anything going over the public internet. This link gives more correct answer. Here is an implementation using SSL.
您的问题是您将 DefaultHttpClient 用于 https
(安全网址).
创建自定义 DefaultHttpClient
Your problem is you are using DefaultHttpClient for https
(secure url).
Create a custom DefaultHttpClient
public static HttpClient createHttpClient()
{
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET);
HttpProtocolParams.setUseExpectContinue(params, true);
SchemeRegistry schReg = new SchemeRegistry();
schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg);
return new DefaultHttpClient(conMgr, params);
}
比更改您的代码如下:
HttpClient httpclient = createHttpClient();
HttpPost httppost = new HttpPost("https://10.0.2.2/insert222.php");
httppost.setEntity(new UrlEncodedFormEntity(data));
HttpResponse response = httpclient.execute(httppost);
如果您有问题,请查看此处
它应该可以工作.
Have a look at here if you have problems
It should work.
这篇关于javax.net.ssl.sslpeerunverifiedexception 无对等证书的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!