本文介绍了javax.net.ssl.sslpeerunverifiedexception 无对等证书的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试通过将数据从 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());
}
}
有人可以帮忙吗?
推荐答案
您的问题是您将 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 无对等证书的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!