避免经常更换证书,或者证书过期问题,在老项目中使用
依赖:
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> <version>4.4.10</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.6</version> </dependency>
HttpUtils:
/** * https 绕过证书 * @param url * @param message * @return * @return */ public static String spost(String url,String message) { String result=null; DefaultHttpClient httpClient = new DefaultHttpClient(); //创建默认的httpClient实例 X509TrustManager xtm = new X509TrustManager() { //创建TrustManager public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {} public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {} public X509Certificate[] getAcceptedIssuers() { return null; } }; try { //TLS1.0与SSL3.0基本上没有太大的差别,可粗略理解为TLS是SSL的继承者,但它们使用的是相同的SSLContext SSLContext ctx = SSLContext.getInstance("SSL"); //使用TrustManager来初始化该上下文,TrustManager只是被SSL的Socket所使用 ctx.init(null, new TrustManager[]{xtm}, null); //创建SSLSocketFactory @SuppressWarnings("deprecation") SSLSocketFactory socketFactory = new SSLSocketFactory(ctx); //SSLConnectionSocketFactory //通过SchemeRegistry将SSLSocketFactory注册到我们的HttpClient上 httpClient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", socketFactory, 443)); httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8"); //创建Httppost方法的实例 HttpPost httpPost = new HttpPost(url); //创建HttpPost StringEntity requestEntity = new StringEntity(message, "utf-8"); requestEntity.setContentEncoding("UTF-8"); httpPost.setHeader("Content-type", "application/json"); httpPost.setEntity(requestEntity); //设置连接时间,与超时时间 RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(30000).setConnectTimeout(5000).build(); httpPost.setConfig(requestConfig); HttpResponse response = httpClient.execute(httpPost); //执行 if(response.getStatusLine().getStatusCode() == 200) { //读取内容 HttpEntity entity = response.getEntity(); //获取响应实体 if (null != entity) { result = EntityUtils.toString(entity, "UTF-8"); } } else log.error("获取temp_id时,认证平台发生内部错误!!"); }catch(Exception e){ log.error("认证失败,原因:[认证系统异常] exception is ",e); }finally{ //释放连接 httpClient.getConnectionManager().shutdown(); //关闭连接,释放资源 } return result; } /** * * @param url * @param message * @param header default : "Content-type", "application/json" * @return * @throws ParseException * @throws IOException * @throws NoSuchAlgorithmException * @throws KeyManagementException */ public static String post(String url,String message) throws ParseException, IOException, NoSuchAlgorithmException, KeyManagementException { String result=null; //创建默认的httpClient实例 DefaultHttpClient httpClient = new DefaultHttpClient(); X509TrustManager xtm = new X509TrustManager() { //创建TrustManager public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {} public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {} public X509Certificate[] getAcceptedIssuers() { return null; } }; //TLS1.0与SSL3.0基本上没有太大的差别,可粗略理解为TLS是SSL的继承者,但它们使用的是相同的SSLContext SSLContext ctx = SSLContext.getInstance("SSL"); //使用TrustManager来初始化该上下文,TrustManager只是被SSL的Socket所使用 ctx.init(null, new TrustManager[]{xtm}, null); //创建SSLSocketFactory @SuppressWarnings("deprecation") SSLSocketFactory socketFactory = new SSLSocketFactory(ctx); //SSLConnectionSocketFactory //通过SchemeRegistry将SSLSocketFactory注册到我们的HttpClient上 httpClient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", socketFactory, 443)); httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8"); //创建HttpPost方法的实例 HttpPost httpPost = new HttpPost(url); //创建HttpPost StringEntity requestEntity = new StringEntity(message, "utf-8"); requestEntity.setContentEncoding("UTF-8"); httpPost.setHeader("Content-type", "application/json"); httpPost.setEntity(requestEntity); //设置连接时间,与超时时间 RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(30000).setConnectTimeout(5000).build(); httpPost.setConfig(requestConfig); HttpResponse response = httpClient.execute(httpPost); //执行 if(response.getStatusLine().getStatusCode() == 200) { //读取内容 HttpEntity entity = response.getEntity(); //获取响应实体 if (null != entity) { result = EntityUtils.toString(entity, "UTF-8"); httpClient.getConnectionManager().shutdown(); //关闭连接,释放资源 } } return result; }