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();
	    }

 

你可能感兴趣的:(java带证书请求)