zoukankan      html  css  js  c++  java
  • javax.net.ssl.SSLHandshakeException sun.security.validator.ValidatorException PK

    javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target


    请求https出现错误, 要使用加密的请求

    class工具:

    class SSLClient extends DefaultHttpClient {
        public SSLClient() throws Exception{
            super();
            SSLContext ctx = SSLContext.getInstance("TLS");
            X509TrustManager tm = new X509TrustManager() {
                @Override
                public void checkClientTrusted(X509Certificate[] chain,
                                               String authType) throws CertificateException {
                }
                @Override
                public void checkServerTrusted(X509Certificate[] chain,
                                               String authType) throws CertificateException {
                }
                @Override
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
            };
            ctx.init(null, new TrustManager[]{tm}, null);
            SSLSocketFactory ssf = new SSLSocketFactory(ctx);
            ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
            ClientConnectionManager ccm = this.getConnectionManager();
            SchemeRegistry sr = ccm.getSchemeRegistry();
            sr.register(new Scheme("https", ssf,443));
        }
    }

    使用:

    public static String sendPost(String url,String paramter){
        Map<String,String> map =new HashMap<>();
        String[] args=paramter.split("&");
    
        for(String str: args){
            String[] arg=str.split("=");
            map.put(arg[0],arg[1]);
        }
        String charset="utf-8";
        HttpClient httpClient = null;
        HttpPost httpPost = null;
        String result = null;
        try{
            httpClient = new SSLClient();
            httpPost = new HttpPost(url);
            //设置参数
            List<NameValuePair> list = new ArrayList<NameValuePair>();
            Iterator iterator = map.entrySet().iterator();
            while(iterator.hasNext()){
                Map.Entry<String,String> elem = (Map.Entry<String, String>) iterator.next();
                list.add(new BasicNameValuePair(elem.getKey(),elem.getValue()));
            }
            if(list.size() > 0){
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,charset);
                httpPost.setEntity(entity);
            }
            HttpResponse response = httpClient.execute(httpPost);
            if(response != null){
                HttpEntity resEntity = response.getEntity();
                if(resEntity != null){
                    result = EntityUtils.toString(resEntity,charset);
                }
            }
        }catch(Exception ex){
            ex.printStackTrace();
        }
        return result;
    }
  • 相关阅读:
    HBase 异步查询导致的死锁和zookeeper通信中断问题追踪与总结[非技术]
    [读书笔记]代码整洁之道读书笔记
    HBase行锁与MVCC分析
    进程、线程、轻量级进程、协程和go中的Goroutine 那些事儿
    上周回顾 - 2012年11.26-12.4
    2012年一个屌丝程序员的学习总结:读书、户外、泡妞、习惯、母猪产后护理
    C#_WinForm接收命令行参数
    SQL常识
    集成.Net / Flex3 & FluorineFX — Part II: The Client
    DB2基本概念
  • 原文地址:https://www.cnblogs.com/yysbolg/p/8565495.html
Copyright © 2011-2022 走看看