| | |
| | | import java.net.MalformedURLException; |
| | | import java.net.URL; |
| | | import java.net.URLConnection; |
| | | import java.security.KeyManagementException; |
| | | import java.security.KeyStore; |
| | | import java.security.KeyStoreException; |
| | | import java.security.NoSuchAlgorithmException; |
| | | import java.security.cert.CertificateException; |
| | | import java.security.cert.X509Certificate; |
| | | import java.util.Iterator; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | |
| | | import org.apache.http.HttpEntity; |
| | | import org.apache.http.HttpResponse; |
| | | import org.apache.http.client.HttpClient; |
| | | import org.apache.http.client.config.RequestConfig; |
| | | 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.client.methods.HttpUriRequest; |
| | | import org.apache.http.conn.ssl.NoopHostnameVerifier; |
| | | import org.apache.http.conn.ssl.SSLConnectionSocketFactory; |
| | | import org.apache.http.conn.ssl.TrustStrategy; |
| | | import org.apache.http.entity.StringEntity; |
| | | import org.apache.http.impl.client.CloseableHttpClient; |
| | | import org.apache.http.impl.client.DefaultHttpClient; |
| | |
| | | } |
| | | |
| | | /** |
| | | * get请求 |
| | | * |
| | | * @param url |
| | | * 请求地址(get请求时参数自己组装到url上) |
| | | * @param headerMap |
| | | * 请求头 |
| | | * @return 响应文本 |
| | | */ |
| | | public static String getUU(String url, Map<String, String> headerMap, String param) { |
| | | // 请求地址,以及参数设置 |
| | | HttpPost post = new HttpPost(url); |
| | | |
| | | SSLContext sslContext = null; |
| | | try { |
| | | sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() { |
| | | @Override |
| | | public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { |
| | | return true; |
| | | } |
| | | }).build(); |
| | | } catch (NoSuchAlgorithmException e) { |
| | | e.printStackTrace(); |
| | | } catch (KeyManagementException e) { |
| | | e.printStackTrace(); |
| | | } catch (KeyStoreException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | CloseableHttpClient client = HttpClients.custom().setSslcontext(sslContext). |
| | | setSSLHostnameVerifier(new NoopHostnameVerifier()).build(); |
| | | |
| | | |
| | | //请求超时时间设置 |
| | | post.setConfig(RequestConfig.custom()// 连接超时时间 |
| | | .setConnectTimeout(5000) |
| | | // 请求超时时间 |
| | | .setConnectionRequestTimeout(5000) |
| | | // Socket读取超时时间 |
| | | .setSocketTimeout(5000) |
| | | // 是否允许重定向 |
| | | .setRedirectsEnabled(false) |
| | | .build()); |
| | | |
| | | //发送请求 |
| | | CloseableHttpResponse response = null; |
| | | try { |
| | | response = client.execute(post); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | log.error("request error. "); |
| | | |
| | | } |
| | | HttpEntity entity = response.getEntity(); |
| | | try { |
| | | if (entity != null) { |
| | | //按指定编码转换结果实体为String类型 |
| | | return EntityUtils.toString(entity, "UTF-8"); |
| | | } |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | return null; |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 获取响应信息(String) |
| | | */ |
| | | public static String getRespString(HttpUriRequest request) { |