public static void main(String[] args) { // TODO Auto-generated method stub SSLContextBuilder builder = new SSLContextBuilder(); try { builder.loadTrustMaterial(null, new TrustSelfSignedStrategy()); SSLConnectionSocketFactory sslFactory = new SSLConnectionSocketFactory(builder.build()); CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslFactory).build(); HttpPost httpPost = new HttpPost("https://*****:*****@api.netkiller.com/v1/withdraw/create.json"); httpPost.addHeader("content-type", "application/json"); httpPost.addHeader("Accept", "application/json"); HttpEntity httpEntity = new StringEntity( "{\"loginname\":\"888666\", \"bankname\":\"中国银行\",\"billno\":\"B040216210517590856\",\"flag\":\"a\",\"amount\":12,\"accountnumber\":\"9555500060007000\",\"accounttype\":\"借记卡\",\"createdate\":\"2016-09-10T20:12:12\",\"remarks\":\"CFD\",\"currency\":\"CNY\",\"accountname\":\"王宝强\",\"branchname\":\"南山支行\",\"bankaddress\":\"深圳市南山区科技园\",\"customerlevel\":2,\"trustlevel\":1}", "UTF-8"); httpPost.setEntity(httpEntity); // HttpGet("https://*****:*****@api.netkiller.com/v1/withdraw/ping.json"); CloseableHttpResponse response = httpclient.execute(httpPost); System.out.println(response.getStatusLine()); HttpEntity entity = response.getEntity(); String responseBody = EntityUtils.toString(entity, "UTF-8"); System.out.println(responseBody.toString()); response.close(); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (KeyStoreException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (KeyManagementException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { } }
/** Gets an http client to perform requests. */ public CloseableHttpClient getHttpClient() { CloseableHttpClient result; try { final SSLContextBuilder builder = new SSLContextBuilder(); if (trustAllMode) builder.loadTrustMaterial(null, new TrustSelfSignedStrategy()); final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build()) { /* This is patched to avoid "Could not generate DH keypair" error with JDK < 1.8 */ protected void prepareSocket(SSLSocket socket) throws IOException { final String[] enabledCipherSuites = socket.getEnabledCipherSuites(); final String version = System.getProperty("java.version"); final List<String> list = new ArrayList<String>(Arrays.asList(enabledCipherSuites)); if (!version.startsWith("1.8")) { list.remove("TLS_DHE_RSA_WITH_AES_128_CBC_SHA"); list.remove("SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA"); list.remove("TLS_DHE_RSA_WITH_AES_256_CBC_SHA"); } socket.setEnabledCipherSuites(list.toArray(new String[list.size()])); } }; result = HttpClients.custom() .setSSLSocketFactory(sslsf) .setRedirectStrategy(new LaxRedirectStrategy()) .useSystemProperties() .build(); } catch (Exception e) { LOG.warning("Could not create custom http client, using default: " + e.getMessage()); result = HttpClients.custom() .setRedirectStrategy(new LaxRedirectStrategy()) .useSystemProperties() .build(); } return result; }
public static synchronized void setGlobalSSLAuth( String keypath, String keypassword, String trustpath, String trustpassword) { // load the stores if defined try { if (trustpath != null && trustpassword != null) { truststore = KeyStore.getInstance(KeyStore.getDefaultType()); try (FileInputStream instream = new FileInputStream(new File(trustpath))) { truststore.load(instream, trustpassword.toCharArray()); } } else truststore = null; if (keypath != null && keypassword != null) { keystore = KeyStore.getInstance(KeyStore.getDefaultType()); try (FileInputStream instream = new FileInputStream(new File(keypath))) { keystore.load(instream, keypassword.toCharArray()); } } else keystore = null; } catch (IOException | NoSuchAlgorithmException | CertificateException | KeyStoreException ex) { log.error("Illegal -D keystore parameters: " + ex.getMessage()); truststore = null; keystore = null; } try { // set up the context SSLContext scxt = null; if (IGNORECERTS) { scxt = SSLContext.getInstance("TLS"); TrustManager[] trust_mgr = new TrustManager[] { new X509TrustManager() { public X509Certificate[] getAcceptedIssuers() { return null; } public void checkClientTrusted(X509Certificate[] certs, String t) {} public void checkServerTrusted(X509Certificate[] certs, String t) {} } }; scxt.init( null, // key manager trust_mgr, // trust manager new SecureRandom()); // random number generator } else { SSLContextBuilder sslbuilder = SSLContexts.custom(); TrustStrategy strat = new LooseTrustStrategy(); if (truststore != null) sslbuilder.loadTrustMaterial(truststore, strat); else sslbuilder.loadTrustMaterial(strat); sslbuilder.loadTrustMaterial(truststore, new LooseTrustStrategy()); if (keystore != null) sslbuilder.loadKeyMaterial(keystore, keypassword.toCharArray()); scxt = sslbuilder.build(); } globalsslfactory = new SSLConnectionSocketFactory(scxt, new NoopHostnameVerifier()); RegistryBuilder rb = RegistryBuilder.<ConnectionSocketFactory>create(); rb.register("https", globalsslfactory); sslregistry = rb.build(); } catch (KeyStoreException | NoSuchAlgorithmException | KeyManagementException | UnrecoverableEntryException e) { log.error("Failed to set key/trust store(s): " + e.getMessage()); sslregistry = null; globalsslfactory = null; } }