java 带证书发送请求 本地证书地址和服务器上证书地址

  /**
	     * 带证书发送请求
	     * */
	    @SuppressWarnings({ "unused", "deprecation" })
		private static String ssl(String url,String data){
	        StringBuffer message = new StringBuffer();
	        try {
	            String mchId = configApi.mchId;//商户id
	            KeyStore keyStore  = KeyStore.getInstance("PKCS12");
	            String certFilePath = "";//证书路径
                
                //服务器上的证书
	            URL urlb = new URL(certFilePath);
	            URLConnection conn = urlb.openConnection();
	            //请求服务器是证书方式
	            // http的连接类
	            HttpURLConnection httpURLConnection = (HttpURLConnection) conn;
	            // 设定请求的方法,默认是GET
	            httpURLConnection.setRequestMethod("GET");
	            // 设置字符编码
	            httpURLConnection.setRequestProperty("Charset", "UTF-8");
	            // 打开到此 URL 引用的资源的通信链接(如果尚未建立这样的连接)。
	            httpURLConnection.connect();
	            InputStream a = httpURLConnection.getInputStream();

	           //本地请求证书使用
//	            FileInputStream instream = new FileInputStream(new File(certFilePath));
	            keyStore.load(a, mchId.toCharArray());
	            SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, mchId.toCharArray()).build();
	            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" }, null, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
	            CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
	            HttpPost httpost = new HttpPost(url);
	            httpost.addHeader("Connection", "keep-alive");
	            httpost.addHeader("Accept", "*/*");
	            httpost.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
	            httpost.addHeader("Host", "api.mch.weixin.qq.com");
	            httpost.addHeader("X-Requested-With", "XMLHttpRequest");
	            httpost.addHeader("Cache-Control", "max-age=0");
	            httpost.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0) ");
	            httpost.setEntity(new StringEntity(data, "UTF-8"));
	            System.out.println("executing request" + httpost.getRequestLine());
	            CloseableHttpResponse response = httpclient.execute(httpost);
	            try {
	                HttpEntity entity = response.getEntity();
	                System.out.println("----------------------------------------");
	                System.out.println(response.getStatusLine());
	                if (entity != null) {
	                    System.out.println("Response content length: " + entity.getContentLength());
	                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(entity.getContent(),"UTF-8"));
	                    String text;
	                    while ((text = bufferedReader.readLine()) != null) {
	                        message.append(text);
	                    }
	                }
	                EntityUtils.consume(entity);
	            } catch (IOException e) {
	                e.printStackTrace();
	            } finally {
	                response.close();
	            }
	        } catch (Exception e1) {
	            e1.printStackTrace();
	        } 
	        return message.toString();
	    }

 

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值