使用httpclient加载证书

12 篇文章 1 订阅
package com.bree.proxy.utils;
 
    /**
    * Created with IntelliJ IDEA.
    * User: victor
    * Date: 13-10-11
    * Time: 下午3:09
    * To change this template use File | Settings | File Templates.
    */
 
    import java.io.File;
 
    import java.io.FileInputStream;
import java.lang.reflect.Method;
import java.security.KeyStore;
import java.util.regex.Pattern;

import javax.net.ssl.SSLContext;
 
    import org.apache.http.HttpEntity;
 
    import org.apache.http.client.methods.CloseableHttpResponse;
 
    import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLContexts;
 
    import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
 
    import org.apache.http.impl.client.CloseableHttpClient;
 
    import org.apache.http.impl.client.HttpClients;
 
    import org.apache.http.util.EntityUtils;
 
 
 
    /**
    * 代码展示了如果使用ssl context创建安全socket连接
    */
 
    public class ClientCustomSSL {
 
        public final static void main(String[] args) throws Exception {
 
            KeyStore trustStore  = KeyStore.getInstance(KeyStore.getDefaultType());
 
            //加载证书文件
 
            FileInputStream instream = new FileInputStream(new File("e://my.store"));
 
            try {
 
                trustStore.load(instream, "123456".toCharArray());
 
            } finally {
 
                instream.close();
 
            }
 
            SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(trustStore).build();
 
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext,
 
                    SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
            
            
 
            CloseableHttpClient httpclient = HttpClients.custom()
 
                    .setSSLSocketFactory(sslsf)
 
                    .build();
 
            try
 
            {
 
                //访问支付宝
                //遇到证书解析报错时增加下面部分代码
            	Method add = sun.security.ec.CurveDB.class.getDeclaredMethod("add", String.class,String.class,
            		       int.class, String.class,String.class, String.class,String.class, 
            		       String.class,String.class,int.class,Pattern.class);
            		     add.setAccessible(true);
            		     Pattern localPattern = Pattern.compile(",|\\[|\\]");
            		     String p="FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFF";
            		     String a="FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFC";
            		     String b="28E9FA9E9D9F5E344D5A9E4BCF6509A7F39789F515AB8F92DDBCBD414D940E93";
            		     String n="FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123";
            		     String gx="32C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1715A4589334C74C7";
            		     String gy="BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0";
            		     add.invoke(null,"SM2", "1.2.156.10197.1.301", 1, p, a, b, n, gx, gy, 1, localPattern);
            		     
            	HttpPost httppost = new HttpPost("https://fpdk.ningxia.chinatax.gov.cn/login.do");
 
                System.out.println("executing request" + httppost.getRequestLine());
 
                CloseableHttpResponse response = httpclient.execute(httppost);
 
                try {
 
                    HttpEntity entity = response.getEntity();
 
                    System.out.println("----------------------------------------");
 
                    System.out.println(response.getStatusLine());
 
                    if (entity != null) {
 
                        System.out.println(EntityUtils.toString(entity));
 
                    }
 
                }catch(Exception e){
                	e.printStackTrace();
                } finally {
 
                    response.close();
 
                }
 
            }catch(Exception e){
            	e.printStackTrace();
            } finally {
 
                httpclient.close();
 
            }
 
        }
 
    }

如有问题,请私信。

tL+8vMr1vbvB96Osx+vO8NPD09q3x7eo08PNvqGjDQoNCsTmz/LP4LnYv86zzKGiSlOyubu3vrO/zrPMoaJKQVZBz+C52L/Os8zI59Do0qrSsr/J0tTBqs+1UVGhow0KDQrX99XfIFFRIDQwNDU0MDIyOQ==

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

liberty888

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值