| | |
| | | import org.apache.http.client.methods.CloseableHttpResponse; |
| | | import org.apache.http.client.methods.HttpGet; |
| | | import org.apache.http.client.methods.HttpPost; |
| | | import org.apache.http.entity.ContentType; |
| | | import org.apache.http.conn.ssl.SSLConnectionSocketFactory; |
| | | import org.apache.http.entity.StringEntity; |
| | | import org.apache.http.impl.client.CloseableHttpClient; |
| | | import org.apache.http.impl.client.HttpClients; |
| | | import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; |
| | | import org.apache.http.message.BasicNameValuePair; |
| | | import org.apache.http.ssl.SSLContexts; |
| | | import org.apache.http.util.EntityUtils; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.net.ssl.SSLContext; |
| | | import java.io.File; |
| | | import java.io.FileInputStream; |
| | | import java.io.IOException; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Set; |
| | | import java.io.InputStream; |
| | | import java.nio.charset.Charset; |
| | | import java.security.KeyStore; |
| | | import java.util.*; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | /** |
| | | * http工具类 |
| | |
| | | * 创建一个httpClient对象 |
| | | */ |
| | | private void getHttpCline(){ |
| | | this.httpClient = HttpClients.createDefault(); |
| | | //1.创建连接池管理器 |
| | | PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(60000, |
| | | TimeUnit.MILLISECONDS); |
| | | connectionManager.setMaxTotal(1000); |
| | | connectionManager.setDefaultMaxPerRoute(50); |
| | | |
| | | //2.创建httpclient对象 |
| | | this.httpClient = HttpClients.custom() |
| | | .setConnectionManager(connectionManager) |
| | | .disableAutomaticRetries() |
| | | .build(); |
| | | } |
| | | |
| | | private void setRequestConfig(){ |
| | | |
| | | private RequestConfig getRequestConfig(){ |
| | | return RequestConfig.custom() |
| | | .setConnectTimeout(60000) |
| | | .setSocketTimeout(60000) |
| | | .build(); |
| | | } |
| | | |
| | | |
| | |
| | | */ |
| | | private void setPostHttpRequset(String url, Map<String, Object> params, Map<String, String> header, String contentType){ |
| | | HttpPost httpPost = new HttpPost(url); |
| | | httpPost.setConfig(this.getRequestConfig()); |
| | | if(null != header){ |
| | | for(String key : header.keySet()){ |
| | | httpPost.setHeader(key, header.get(key)); |
| | |
| | | try { |
| | | switch (contentType){ |
| | | case "form": |
| | | httpPost.setEntity(new UrlEncodedFormEntity(list)); |
| | | httpPost.setEntity(new UrlEncodedFormEntity(list, "UTF-8")); |
| | | break; |
| | | case "json": |
| | | ObjectMapper objectMapper = new ObjectMapper(); |
| | | String s =objectMapper.writeValueAsString(params); |
| | | System.err.println(s); |
| | | httpPost.setEntity(new StringEntity(s, ContentType.APPLICATION_JSON)); |
| | | httpPost.setEntity(new StringEntity(s, Charset.forName("UTF-8"))); |
| | | break; |
| | | } |
| | | this.getHttpCline(); |
| | | if(null == this.httpClient){ |
| | | this.getHttpCline(); |
| | | } |
| | | httpResponse = this.httpClient.execute(httpPost); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | |
| | | } |
| | | } |
| | | this.getHttpCline(); |
| | | if(null == this.httpClient){ |
| | | this.getHttpCline(); |
| | | } |
| | | try { |
| | | httpResponse = this.httpClient.execute(httpGet); |
| | | } catch (IOException e) { |
| | |
| | | if(httpResponse.getStatusLine().getStatusCode() == 200){ |
| | | try { |
| | | content = EntityUtils.toString(httpResponse.getEntity()); |
| | | this.close(); |
| | | return content; |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | this.close(); |
| | | } |
| | | } |
| | | if(httpResponse.getStatusLine().getStatusCode() == 201){ |
| | | content = "{\"status\":201}"; |
| | | this.close(); |
| | | return content; |
| | | }else{ |
| | | try { |
| | | content = "返回状态码:" + httpResponse.getStatusLine() + "。" + EntityUtils.toString(httpResponse.getEntity()); |
| | | System.err.println("返回状态码:" + httpResponse.getStatusLine() + "。"); |
| | | content = EntityUtils.toString(httpResponse.getEntity()); |
| | | this.close(); |
| | | return content; |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | this.close(); |
| | |
| | | try { |
| | | httpPost.setEntity(new StringEntity(xml, "UTF-8")); |
| | | this.getHttpCline(); |
| | | if(null == this.httpClient){ |
| | | this.getHttpCline(); |
| | | } |
| | | httpResponse = this.httpClient.execute(httpPost); |
| | | String content = null; |
| | | if(httpResponse.getStatusLine().getStatusCode() == 200){ |
| | | try { |
| | | content = EntityUtils.toString(httpResponse.getEntity(), "UTF-8"); |
| | | this.close(); |
| | | return content; |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | this.close(); |
| | | } |
| | | }else{ |
| | | try { |
| | | content = "返回状态码:" + httpResponse.getStatusLine() + "。" + EntityUtils.toString(httpResponse.getEntity()); |
| | | this.close(); |
| | | return content; |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | this.close(); |
| | | } |
| | | } |
| | | this.close(); |
| | | return content; |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | this.close(); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 请求https发送XML请求 |
| | | * @param url 接口路径 |
| | | * @param xml 内容 |
| | | * @param header 请求头 |
| | | * @param certPassword 证书密码 |
| | | * @param certPath 证书路径 |
| | | * @param certType 证书类型 |
| | | * @return |
| | | * @throws Exception |
| | | */ |
| | | public String pushHttpsRequsetXml(String url, String xml, Map<String, String> header, String certPassword, String certPath, String certType) throws Exception{ |
| | | HttpPost httpPost = new HttpPost(url); |
| | | for(String key : header.keySet()){ |
| | | httpPost.setHeader(key, header.get(key)); |
| | | } |
| | | httpPost.setHeader("Content-Type", "application/xml"); |
| | | try { |
| | | httpPost.setEntity(new StringEntity(xml, "UTF-8")); |
| | | this.getHttpCline(); |
| | | this.initCert(certPassword, certPath, certType); |
| | | httpResponse = this.httpClient.execute(httpPost); |
| | | String content = null; |
| | | if(httpResponse.getStatusLine().getStatusCode() == 200){ |
| | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 初始化https对象(带证书) |
| | | * @param key 证书密码 |
| | | * @param certPath 证书路径 |
| | | * @param certType 证书类型 |
| | | * @throws Exception |
| | | */ |
| | | private void initCert(String key, String certPath, String certType) throws Exception { |
| | | KeyStore keyStore = KeyStore.getInstance(certType); |
| | | // ClassPathResource cp = new ClassPathResource(certPath); |
| | | InputStream inputStream = new FileInputStream(new File(certPath)); |
| | | // InputStream instream = cp.getInputStream(); |
| | | try { |
| | | keyStore.load(inputStream, key.toCharArray()); |
| | | } finally { |
| | | inputStream.close(); |
| | | } |
| | | SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, key.toCharArray()).build(); |
| | | SSLConnectionSocketFactory sslsf = |
| | | new SSLConnectionSocketFactory(sslcontext, new String[] {"TLSv1"}, null, |
| | | SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER); |
| | | this.httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build(); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 关闭资源 |
| | | */ |
| | | private void close(){ |
| | | try { |
| | | httpClient.close(); |
| | | httpResponse.close(); |
| | | if(null != httpClient){ |
| | | httpClient.close(); |
| | | } |
| | | if(null != httpResponse){ |
| | | httpResponse.close(); |
| | | } |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | }finally { |