diff --git a/src/main/java/cn/chain33/javasdk/utils/HttpUtil.java b/src/main/java/cn/chain33/javasdk/utils/HttpUtil.java index 73f23ec..35a2428 100644 --- a/src/main/java/cn/chain33/javasdk/utils/HttpUtil.java +++ b/src/main/java/cn/chain33/javasdk/utils/HttpUtil.java @@ -25,15 +25,15 @@ import org.apache.http.ssl.SSLContextBuilder; public class HttpUtil { - + private static CloseableHttpClient client = null; - + private static CloseableHttpClient httpsClient = null; private static RequestConfig requestConfig = null; - + private static final String DEFAULT_CHARSET = "UTF-8"; - + private static final int DEFAULT_TIME_OUT = 400000; static{ client = HttpClientBuilder.create().build(); @@ -44,84 +44,80 @@ public class HttpUtil { requestConfig = configBuilder.build(); httpsClient = createSSLInsecureClient(); } - - @SuppressWarnings("deprecation") - public static CloseableHttpClient createSSLInsecureClient() { - try { - SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() { - // 默认信任所有证书 - public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { - return true; - } - }).build(); - // AllowAllHostnameVerifier: 这种方式不对主机名进行验证,验证功能被关闭,是个空操作(域名验证) - SSLConnectionSocketFactory sslcsf = new SSLConnectionSocketFactory(sslContext, - SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); - return HttpClients.custom().setSSLSocketFactory(sslcsf).build(); - } catch (KeyManagementException e) { - e.printStackTrace(); - } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); - } catch (KeyStoreException e) { - e.printStackTrace(); - } - return HttpClients.createDefault(); - } - - private static String getContent(HttpEntity entity,String charset){ - String content = null; - if(entity != null){ - try { - InputStream in = entity.getContent(); - InputStreamReader inputStreamReader = new InputStreamReader(in,charset); - BufferedReader reader = new BufferedReader(inputStreamReader); - StringBuffer stringBuffer = new StringBuffer(); - String line = null; - while((line = reader.readLine()) != null){ - stringBuffer.append(line); - stringBuffer.append("\r\n"); + + @SuppressWarnings("deprecation") + public static CloseableHttpClient createSSLInsecureClient() { + try { + SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() { + // 默认信任所有证书 + public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { + return true; } - content = stringBuffer.toString(); - } catch (Exception e) { - e.printStackTrace(); + }).build(); + // AllowAllHostnameVerifier: 这种方式不对主机名进行验证,验证功能被关闭,是个空操作(域名验证) + SSLConnectionSocketFactory sslcsf = new SSLConnectionSocketFactory(sslContext, + SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); + return HttpClients.custom().setSSLSocketFactory(sslcsf).build(); + } catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException e) { + e.printStackTrace(); + } + return HttpClients.createDefault(); + } + + private static String getContent(HttpEntity entity, String charset) { + if (entity == null) { + throw new RuntimeException("请求失败"); + } + try { + InputStream in = entity.getContent(); + InputStreamReader inputStreamReader = new InputStreamReader(in, charset); + BufferedReader reader = new BufferedReader(inputStreamReader); + StringBuffer stringBuffer = new StringBuffer(); + String line = null; + while ((line = reader.readLine()) != null) { + stringBuffer.append(line); + stringBuffer.append("\r\n"); } + return stringBuffer.toString(); + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException(e); } - return content; } - - + + public static String httpPostBody(String url, String jsonString) { - String content = null; + String content; HttpPost post = new HttpPost(url); try { post.setConfig(requestConfig); post.addHeader("Content-Type", "application/json"); - post.setEntity(new StringEntity(jsonString,DEFAULT_CHARSET)); + post.setEntity(new StringEntity(jsonString, DEFAULT_CHARSET)); HttpResponse response = client.execute(post); HttpEntity entity = response.getEntity(); - content = getContent(entity,DEFAULT_CHARSET); + content = getContent(entity, DEFAULT_CHARSET); post.releaseConnection(); } catch (Exception e) { e.printStackTrace(); - return null; + throw new RuntimeException(e); } return content; } - + public static String httpsPostBody(String url, String jsonString) { String content = null; HttpPost post = new HttpPost(url); try { post.setConfig(requestConfig); post.addHeader("Content-Type", "application/json"); - post.setEntity(new StringEntity(jsonString,DEFAULT_CHARSET)); + post.setEntity(new StringEntity(jsonString, DEFAULT_CHARSET)); HttpResponse response = httpsClient.execute(post); HttpEntity entity = response.getEntity(); - content = getContent(entity,DEFAULT_CHARSET); + content = getContent(entity, DEFAULT_CHARSET); post.releaseConnection(); } catch (Exception e) { e.printStackTrace(); - return null; + throw new RuntimeException(e); } return content; }