|  |  |  | 
|---|
|  |  |  | package com.matrix.component.wechat.externalInterface.common; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.matrix.core.tools.WebUtil; | 
|---|
|  |  |  | import com.matrix.system.common.bean.BusParameterSettings; | 
|---|
|  |  |  | import com.matrix.system.common.constance.AppConstance; | 
|---|
|  |  |  | import com.matrix.system.common.dao.BusParameterSettingsDao; | 
|---|
|  |  |  | import com.thoughtworks.xstream.XStream; | 
|---|
|  |  |  | import com.thoughtworks.xstream.io.xml.DomDriver; | 
|---|
|  |  |  | import com.thoughtworks.xstream.io.xml.XmlFriendlyNameCoder; | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | Logger log=Logger.getLogger(HttpRequest.class); | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //表示请求器是否已经做了初始化工作 | 
|---|
|  |  |  | private boolean hasInit = false; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //HTTP请求器 | 
|---|
|  |  |  | private CloseableHttpClient httpClient; | 
|---|
|  |  |  | //证书地址 | 
|---|
|  |  |  | private String  certLocalPath; | 
|---|
|  |  |  | //商户号 | 
|---|
|  |  |  | private String mchId; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public HttpsRequest2() throws UnrecoverableKeyException, KeyManagementException, NoSuchAlgorithmException, KeyStoreException, IOException { | 
|---|
|  |  |  | init(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private void init() throws IOException, KeyStoreException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyManagementException { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | KeyStore keyStore = KeyStore.getInstance("PKCS12"); | 
|---|
|  |  |  | FileInputStream instream = new FileInputStream(new File(WechatConfigure.certLocalPath));//加载本地的证书进行https加密传输 | 
|---|
|  |  |  | KeyStore keyStore = KeyStore.getInstance("PKCS12"); | 
|---|
|  |  |  | FileInputStream instream = new FileInputStream(new File(certLocalPath));//加载本地的证书进行https加密传输 | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | keyStore.load(instream,WechatConfigure.certPassword.toCharArray());//设置证书密码 | 
|---|
|  |  |  | keyStore.load(instream, mchId.toCharArray());//设置证书密码 | 
|---|
|  |  |  | } catch (CertificateException e) { | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  | } catch (NoSuchAlgorithmException e) { | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | // Trust own CA and all self-signed certs | 
|---|
|  |  |  | SSLContext sslcontext = SSLContexts.custom() | 
|---|
|  |  |  | .loadKeyMaterial(keyStore, WechatConfigure.certPassword.toCharArray()) | 
|---|
|  |  |  | .loadKeyMaterial(keyStore, mchId.toCharArray()) | 
|---|
|  |  |  | .build(); | 
|---|
|  |  |  | // Allow TLSv1 protocol only | 
|---|
|  |  |  | SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory( | 
|---|
|  |  |  | 
|---|
|  |  |  | null, | 
|---|
|  |  |  | SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER); | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | httpClient = HttpClients.custom() | 
|---|
|  |  |  |  | 
|---|
|  |  |  | httpClient = HttpClients.custom() | 
|---|
|  |  |  | .setSSLSocketFactory(sslsf) | 
|---|
|  |  |  | .build(); | 
|---|
|  |  |  | // httpClient = HttpClients.custom().build(); | 
|---|
|  |  |  | // httpClient = HttpClients.custom().build(); | 
|---|
|  |  |  | //根据默认超时限制初始化requestConfig | 
|---|
|  |  |  | requestConfig = RequestConfig.custom().setSocketTimeout(socketTimeout).setConnectTimeout(connectTimeout).build(); | 
|---|
|  |  |  | hasInit = true; | 
|---|
|  |  |  | 
|---|
|  |  |  | httpPost.setConfig(requestConfig); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | log.info("executing request" + httpPost.getRequestLine()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | HttpResponse response = httpClient.execute(httpPost); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | HttpEntity entity = response.getEntity(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | result = EntityUtils.toString(entity, "UTF-8"); | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | log.info("POST back date:" + httpPost.getRequestLine()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } catch (ConnectionPoolTimeoutException e) { | 
|---|
|  |  |  | //            log.e("http get throw ConnectionPoolTimeoutException(wait time out)"); | 
|---|
|  |  |  | 
|---|
|  |  |  | //            log.e("http get throw SocketTimeoutException"); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  | //            log.e("http get throw Exception"); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } finally { | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param socketTimeout 连接时长,默认10秒 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public void setSocketTimeout(int socketTimeout) { | 
|---|
|  |  |  | this.socketTimeout = socketTimeout; | 
|---|
|  |  |  | this.socketTimeout = socketTimeout; | 
|---|
|  |  |  | resetRequestConfig(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param connectTimeout 传输时长,默认30秒 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public void setConnectTimeout(int connectTimeout) { | 
|---|
|  |  |  | this.connectTimeout = connectTimeout; | 
|---|
|  |  |  | this.connectTimeout = connectTimeout; | 
|---|
|  |  |  | resetRequestConfig(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param requestConfig 设置HttpsRequest的请求器配置 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public void setRequestConfig(RequestConfig requestConfig) { | 
|---|
|  |  |  | this.requestConfig = requestConfig; | 
|---|
|  |  |  | this.requestConfig = requestConfig; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public String getMchId() { | 
|---|
|  |  |  | return mchId; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public void setMchId(String mchId) { | 
|---|
|  |  |  | this.mchId = mchId; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public String getCertLocalPath() { | 
|---|
|  |  |  | return certLocalPath; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public void setCertLocalPath(String certLocalPath) { | 
|---|
|  |  |  | this.certLocalPath = certLocalPath; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|