javax.net.ssl​​.sslpeerunverifiedexception没有同行证书 [英] javax.net.ssl.sslpeerunverifiedexception no peer certificate

查看:141
本文介绍了javax.net.ssl​​.sslpeerunverifiedexception没有同行证书的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个Android应用程序发布数据到PHP服务器中插入一条记录到MySQL。我已经加入了INTERNET权限的Andr​​oidManifest.xml

我得到 javax.net.ssl​​.SSLPeerUnverifiedException:没有对方的证书

Android的code

 私人无效送出数据(ArrayList中<的NameValuePair>数据)
{
    尝试
    {
        HttpClient的HttpClient的=新DefaultHttpClient();
        HttpPost httppost =新HttpPost(https://10.0.2.2/insert222.php);
        httppost.setEntity(新UrlEn codedFormEntity(数据));
        HTT presponse响应= httpclient.execute(httppost);

    }
    赶上(例外五){
        // TODO:处理异常
        Log.e(log_tag,错误:+ e.toString());
    }
}
 

谁能帮助?

解决方案
  

警告:不要在生产$ C $实现本C你永远要你做的不完全信任的网络上使用。尤其是东西去在公共互联网。 此链接提供了更多的正确的答案。 这里使用SSL 是一种实现。

您的问题是,你正在使用DefaultHttpClient为 HTTPS (安全URL)。
创建自定义DefaultHttpClient

 公共静态的HttpClient createHttpClient()
{
    的HttpParams PARAMS =新BasicHttpParams();
    HttpProtocolParams.setVersion(参数,可以HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(参数,可以HTTP.DEFAULT_CONTENT_CHARSET);
    HttpProtocolParams.setUseExpectContinue(参数,可以真正的);

    SchemeRegistry schReg =新SchemeRegistry();
    schReg.register(新计划(HTTP,PlainSocketFactory.getSocketFactory(),80));
    schReg.register(新计划(https开头,SSLSocketFactory.getSocketFactory(),443));
    ClientConnectionManager conMgr =新ThreadSafeClientConnManager(参数,可以schReg);

    返回新DefaultHttpClient(co​​nMgr,则params);
}
 

不是改变你的code如下:

 的HttpClient的HttpClient = createHttpClient();
        HttpPost httppost =新HttpPost(https://10.0.2.2/insert222.php);
        httppost.setEntity(新UrlEn codedFormEntity(数据));
        HTT presponse响应= httpclient.execute(httppost);
 

有一个在这里 的,如果你有问题_ 它应该工作。

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

I get javax.net.ssl.SSLPeerUnverifiedException: No peer certificate

Android code

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());
    }
}

Can anyone help?

解决方案

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.

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);
}

Than change your code as follows:

        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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆