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;
    }
  • 相关阅读:
    input上传限定文件类型
    扫描二维码自动识别手机系统(Android/IOS)
    css/html/Javascript
    移动端容易碰到的点击穿透的坑
    洛谷P3387 【模板】缩点
    洛谷P1137 旅行计划
    洛谷P2324 [SCOI2005]骑士精神
    洛谷P2571 [SCOI2010]传送带
    BZOJ4300: 绝世好题
    [洛谷P1966] 火柴排队
  • 原文地址:https://www.cnblogs.com/yysbolg/p/8565495.html
Copyright © 2011-2022 走看看