SSL没有对等证书Android

时间:2018-02-09 10:51:01

标签: java android ssl ssl-certificate

我有一些设备有问题,而且错误说SSL问题。当我检查我的网站时,那里没有问题。这是我从数据库中获取数据的代码

public class HttpServicesClass {
    public int responseCode;    
    public String message;    
    public String response;    
    public ArrayList<NameValuePair> ArrayListParams;    
    public ArrayList <NameValuePair> headers;    
    public String UrlHolder;    
    public String getResponse()
    {
        return response;
    }    
    public String getErrorMessage()
    {
        return message;
    }    
    public int getResponseCode()
    {
        return responseCode;
    }    
    public HttpServicesClass(String url)
    {
        HttpServicesClass.this.UrlHolder = url;

        ArrayListParams = new ArrayList<NameValuePair>();

        headers = new ArrayList<NameValuePair>();
    }    
    public void AddParam(String name, String value)
    {
        ArrayListParams.add(new BasicNameValuePair(name, value));
    }   
    public void AddHeader(String name, String value)
    {
        headers.add(new BasicNameValuePair(name, value));
    }    
    public void ExecuteGetRequest() throws Exception
    {
        String MixParams = "";

        if(!ArrayListParams.isEmpty())
        {
            MixParams += "?";

            for(NameValuePair p : ArrayListParams)
            {
                String paramString = p.getName() + "=" + URLEncoder.encode(p.getValue(),"UTF-8");

                if(MixParams.length() > 1)
                {
                    MixParams  +=  "&" + paramString;
                }
                else
                {
                    MixParams += paramString;
                }
            }
        }    
        HttpGet httpGet = new HttpGet(UrlHolder + MixParams);  
        for(NameValuePair h : headers)
        {
            httpGet.addHeader(h.getName(), h.getValue());
        }    
        executeRequest(httpGet, UrlHolder);
    }   
    public void ExecutePostRequest() throws Exception
    {
        HttpPost httpPost = new HttpPost(UrlHolder);
        for(NameValuePair h : headers)
        {
            httpPost.addHeader(h.getName(), h.getValue());
        }    
        if(!ArrayListParams.isEmpty())
        {
            httpPost.setEntity(new UrlEncodedFormEntity(ArrayListParams, HTTP.UTF_8));
        }    
        executeRequest(httpPost, UrlHolder);
    }    
    private void executeRequest(HttpUriRequest request, String url)
    {
        HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;

        HttpParams httpParameters = new BasicHttpParams();    
        HttpConnectionParams.setConnectionTimeout(httpParameters, 10000);    
        HttpConnectionParams.setSoTimeout(httpParameters, 10000);    
        HttpClient httpClient = new DefaultHttpClient(httpParameters);    
        SchemeRegistry registry = new SchemeRegistry();
        SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
        socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        registry.register(new Scheme("https", socketFactory, 443));
        SingleClientConnManager mgr = new SingleClientConnManager(httpParameters, registry);
        HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
        new DefaultHttpClient(mgr, httpParameters);
        HttpResponse httpResponse;
        try
        {
            httpResponse = httpClient.execute(request);
            responseCode = httpResponse.getStatusLine().getStatusCode();
            message = httpResponse.getStatusLine().getReasonPhrase();

            HttpEntity entity = httpResponse.getEntity();
            if (entity != null)
            {
                InputStream inputStream = entity.getContent();

                response = convertStreamToString(inputStream);

                inputStream.close();
            }
        }
        catch (ClientProtocolException e)
        {
            httpClient.getConnectionManager().shutdown();
            e.printStackTrace();
        }
        catch (IOException e)
        {
            httpClient.getConnectionManager().shutdown();
            e.printStackTrace();
        }
    }

    private String convertStreamToString(InputStream is)
    {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));

        StringBuilder stringBuilder = new StringBuilder();

        String line = null;
        try
        {
            while ((line = bufferedReader.readLine()) != null)
            {
                stringBuilder.append(line + "\n");
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                is.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
        return stringBuilder.toString();
    }

}

在尝试显示数据库数据时,它会给我错误

  

W / System.err:javax.net.ssl.SSLPeerUnverifiedException:没有对等   证书

很多参考我尝试但仍然给我null这个问题 Safely fixing: javax.net.ssl.SSLPeerUnverifiedException: No peer certificate

error javax.net.ssl.SSLPeerUnverifiedException: No peer certificate and SSLNullSession.java:104

任何人都可以帮我解决这个问题

修改

完整错误日志

  

02-10 07:52:36.565 2621-3000 / com.network.poeja.checkops W / System.err:   javax.net.ssl.SSLPeerUnverifiedException:没有对等证书02-10   07:52:36.665 2621-3000 / com.network.poeja.checkops W / System.err:at   com.android.org.conscrypt.SSLSessionImpl.getPeerCertificates(SSLSessionImpl.java:146)   02-10 07:52:36.665 2621-3000 / com.network.poeja.checkops W / System.err:   在   org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:93)   02-10 07:52:36.675 2621-3000 / com.network.poeja.checkops W / System.err:   在   org.apache.http.conn.ssl.SSLSocketFactory.createSocket(SSLSocketFactory.java:388)   02-10 07:52:36.675 2621-3000 / com.network.poeja.checkops W / System.err:   在   org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:165)   02-10 07:52:36.675 2621-3000 / com.network.poeja.checkops W / System.err:   在   org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)   02-10 07:52:36.675 2621-3000 / com.network.poeja.checkops W / System.err:   在   org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)   02-10 07:52:36.675 2621-3000 / com.network.poeja.checkops W / System.err:   在   org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)   02-10 07:52:36.675 2621-3000 / com.network.poeja.checkops W / System.err:   在   org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)   02-10 07:52:36.675 2621-3000 / com.network.poeja.checkops W / System.err:   在   org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)   02-10 07:52:36.675 2621-3000 / com.network.poeja.checkops W / System.err:   在   org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)   02-10 07:52:36.675 2621-3000 / com.network.poeja.checkops W / System.err:   在   com.network.poeja.checkops.HttpServicesClass.executeRequest(HttpServicesClass.java:152)   02-10 07:52:36.685 2621-3000 / com.network.poeja.checkops W / System.err:   在   com.network.poeja.checkops.HttpServicesClass.ExecutePostRequest(HttpServicesClass.java:128)   02-10 07:52:36.685 2621-3000 / com.network.poeja.checkops W / System.err:   在   com.network.poeja.checkops.SecondFragment $ GetHttpResponse.doInBackground(SecondFragment.java:101)   02-10 07:52:36.685 2621-3000 / com.network.poeja.checkops W / System.err:   在   com.network.poeja.checkops.SecondFragment $ GetHttpResponse.doInBackground(SecondFragment.java:78)   02-10 07:52:36.685 2621-3000 / com.network.poeja.checkops W / System.err:   在android.os.AsyncTask $ 2.call(AsyncTask.java:288)02-10 07:52:36.685   2621-3000 / com.network.poeja.checkops W / System.err:at   java.util.concurrent.FutureTask.run(FutureTask.java:237)02-10   07:52:36.685 2621-3000 / com.network.poeja.checkops W / System.err:at   android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231)02-10   07:52:36.685 2621-3000 / com.network.poeja.checkops W / System.err:at   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)   02-10 07:52:36.685 2621-3000 / com.network.poeja.checkops W / System.err:   在   java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:587)   02-10 07:52:36.685 2621-3000 / com.network.poeja.checkops W / System.err:   在java.lang.Thread.run(Thread.java:841)02-10 07:52:36.745   2621-3000 / com.network.poeja.checkops W / System.err:   java.lang.RuntimeException:无法在线程内创建处理程序   没有调用Looper.prepare()02-10 07:52:36.785   2621-3000 / com.network.poeja.checkops W / System.err:at   android.os.Handler。(Handler.java:200)02-10 07:52:36.785   2621-3000 / com.network.poeja.checkops W / System.err:at   android.os.Handler。(Handler.java:114)02-10 07:52:36.785   2621-3000 / com.network.poeja.checkops W / System.err:at   android.widget.Toast $ TN。(Toast.java:327)02-10 07:52:36.785   2621-3000 / com.network.poeja.checkops W / System.err:at   android.widget.Toast。(Toast.java:92)02-10 07:52:36.785   2621-3000 / com.network.poeja.checkops W / System.err:at   android.widget.Toast.makeText(Toast.java:241)02-10 07:52:36.785   2621-3000 / com.network.poeja.checkops W / System.err:at   com.network.poeja.checkops.SecondFragment $ GetHttpResponse.doInBackground(SecondFragment.java:132)   02-10 07:52:36.785 2621-3000 / com.network.poeja.checkops W / System.err:   在   com.network.poeja.checkops.SecondFragment $ GetHttpResponse.doInBackground(SecondFragment.java:78)   02-10 07:52:36.785 2621-3000 / com.network.poeja.checkops W / System.err:   在android.os.AsyncTask $ 2.call(AsyncTask.java:288)02-10 07:52:36.785   2621-3000 / com.network.poeja.checkops W / System.err:at   java.util.concurrent.FutureTask.run(FutureTask.java:237)02-10   07:52:36.785 2621-3000 / com.network.poeja.checkops W / System.err:at   android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231)02-10   07:52:36.785 2621-3000 / com.network.poeja.checkops W / System.err:at   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)   02-10 07:52:36.785 2621-3000 / com.network.poeja.checkops W / System.err:   在   java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:587)   02-10 07:52:36.785 2621-3000 / com.network.poeja.checkops W / System.err:   在java.lang.Thread.run(Thread.java:841)

1 个答案:

答案 0 :(得分:0)

找到了使SSLSocket成为私有类的答案,就像这样?

public class MySSLSocketFactory extends SSLSocketFactory {
    private SSLContext sslContext = SSLContext.getInstance("TLS");

    private MySSLSocketFactory(KeyStore truststore) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {
        super(truststore);

        TrustManager tm = new X509TrustManager() {
            public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            }

            public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            }

            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        };

        sslContext.init(null, new TrustManager[] { tm }, null);
    }

    @Override
    public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException, UnknownHostException {
        return sslContext.getSocketFactory().createSocket(socket, host, port, autoClose);
    }

    @Override
    public Socket createSocket() throws IOException {
        return sslContext.getSocketFactory().createSocket();
    }
}