Skip to content

Instantly share code, notes, and snippets.

@sqzr
Last active April 16, 2020 17:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sqzr/75dbd73f3fd0185ff758474faabdbfc8 to your computer and use it in GitHub Desktop.
Save sqzr/75dbd73f3fd0185ff758474faabdbfc8 to your computer and use it in GitHub Desktop.
test.java
public String TransferRestTemplate(String url,String data) throws KeyStoreException, IOException, CertificateException, NoSuchAlgorithmException, UnrecoverableKeyException, KeyManagementException {
KeyStore keyStore = KeyStore.getInstance("PKCS12");
FileInputStream instream = new FileInputStream(new File(merchantInfo.getPfx()));
keyStore.load(instream, merchantInfo.getId().toCharArray());
// Trust own CA and all self-signed certs
SSLContext sslcontext = SSLContextBuilder.create()
.loadKeyMaterial(keyStore, merchantInfo.getId().toCharArray())
.build();
// Allow TLSv1 protocol only
HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[]{"TLSv1"},
null,hostnameVerifier);
CloseableHttpClient httpclient = HttpClients.custom()
.setSSLSocketFactory(sslsf)
.build();
HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(httpclient);
RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory);
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.add("Connection", "keep-alive");
requestHeaders.add("Accept", "*/*");
requestHeaders.add("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
requestHeaders.add("Host", "api.mch.weixin.qq.com");
requestHeaders.add("X-Requested-With", "XMLHttpRequest");
requestHeaders.add("Cache-Control", "max-age=0");
requestHeaders.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0) ");
org.springframework.http.HttpEntity<String> requestEntity =
new org.springframework.http.HttpEntity(new StringEntity(data, "UTF-8"),requestHeaders);
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
String sttr = response.getBody();
return sttr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment