Commit 1edc5eed authored by 张永's avatar 张永

11

parent ec76a699
...@@ -12,7 +12,12 @@ import java.net.HttpURLConnection; ...@@ -12,7 +12,12 @@ import java.net.HttpURLConnection;
import java.net.Proxy; import java.net.Proxy;
import java.net.URL; import java.net.URL;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.security.KeyManagementException;
import java.security.KeyStore; 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.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
...@@ -29,15 +34,16 @@ import org.apache.http.client.config.RequestConfig; ...@@ -29,15 +34,16 @@ import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.entity.StringEntity; import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts; import org.apache.http.ssl.SSLContexts;
import org.apache.http.ssl.TrustStrategy;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
import com.egolm.common.exception.HttpRequestException; import com.egolm.common.exception.HttpRequestException;
/** /**
* *
* @author 曲欣亮 * @author 曲欣亮
...@@ -49,37 +55,62 @@ public class HttpsUtil { ...@@ -49,37 +55,62 @@ public class HttpsUtil {
static { static {
RequestConfig.Builder configBuilder = RequestConfig.custom(); RequestConfig.Builder configBuilder = RequestConfig.custom();
// 设置连接超时 // 设置连接超时
configBuilder.setConnectTimeout(10*1000); configBuilder.setConnectTimeout(10 * 1000);
// 设置读取超时 // 设置读取超时
configBuilder.setSocketTimeout(10*10000); configBuilder.setSocketTimeout(10 * 10000);
requestConfig = configBuilder.build(); requestConfig = configBuilder.build();
} }
public static CloseableHttpClient trustAll() {
// 配置,发送https请求时,忽略ssl证书认证(否则会报错没有证书)
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();
}
//创建httpClient
CloseableHttpClient client = HttpClients.custom().setSSLContext(sslContext)
.setSSLHostnameVerifier(new NoopHostnameVerifier()).build();
return client;
}
/** /**
* 发送无参数的GET请求 * 发送无参数的GET请求
*/ */
public static String doGet(String url) { public static String doGet(String url) {
return doGet(url,null); return doGet(url, null);
} }
/** /**
* 发送有参数的GET请求,参数为MAP key-value格式 * 发送有参数的GET请求,参数为MAP key-value格式
*/ */
public static String doGet(String url, Map<String, String> params) { public static String doGet(String url, Map<String, String> params) {
List<String> paramList=new ArrayList<String>(); List<String> paramList = new ArrayList<String>();
if(params!=null){ if (params != null) {
for (String key : params.keySet()) { for (String key : params.keySet()) {
paramList.add(key+"="+params.get(key)); paramList.add(key + "=" + params.get(key));
} }
if(paramList.size()>0){ if (paramList.size() > 0) {
if(url.indexOf("?")==-1){ if (url.indexOf("?") == -1) {
url=url+"?"+StringUtil.join("&",paramList); url = url + "?" + StringUtil.join("&", paramList);
}else{ } else {
url=url+"&"+StringUtil.join("&",paramList); url = url + "&" + StringUtil.join("&", paramList);
} }
} }
} }
System.out.println("doGet--"+url); System.out.println("doGet--" + url);
CloseableHttpClient httpclient = HttpClients.createDefault(); CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url.replaceAll(" ", "%20")); HttpGet httpGet = new HttpGet(url.replaceAll(" ", "%20"));
httpGet.setConfig(requestConfig); httpGet.setConfig(requestConfig);
...@@ -95,14 +126,14 @@ public class HttpsUtil { ...@@ -95,14 +126,14 @@ public class HttpsUtil {
throw new HttpRequestException(e); throw new HttpRequestException(e);
} finally { } finally {
try { try {
if(entity!=null){ if (entity != null) {
EntityUtils.consume(entity); EntityUtils.consume(entity);
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
try { try {
if(response!=null){ if (response != null) {
response.close(); response.close();
} }
} catch (IOException e) { } catch (IOException e) {
...@@ -116,14 +147,18 @@ public class HttpsUtil { ...@@ -116,14 +147,18 @@ public class HttpsUtil {
} }
} }
} }
public static String get(String requestUrl, Map<String, Object> parameters, Map<String, String> header, Proxy proxy, SSLSocketFactory sslSocketFactory) throws HttpRequestException {
public static String get(String requestUrl, Map<String, Object> parameters, Map<String, String> header, Proxy proxy,
SSLSocketFactory sslSocketFactory) throws HttpRequestException {
HttpsURLConnection connection = null; HttpsURLConnection connection = null;
try { try {
String requestBody = HttpUtil.toQueryString(parameters, "utf-8"); String requestBody = HttpUtil.toQueryString(parameters, "utf-8");
requestUrl = requestUrl + (requestUrl.contains("?") ? (requestUrl.endsWith("&") ? "" : "&") : "?") + requestBody; requestUrl = requestUrl + (requestUrl.contains("?") ? (requestUrl.endsWith("&") ? "" : "&") : "?")
+ requestBody;
URL GET_URL = new URL(requestUrl); URL GET_URL = new URL(requestUrl);
connection = (HttpsURLConnection) (proxy == null ? GET_URL.openConnection() : GET_URL.openConnection(proxy)); connection = (HttpsURLConnection) (proxy == null ? GET_URL.openConnection()
if(sslSocketFactory != null) { : GET_URL.openConnection(proxy));
if (sslSocketFactory != null) {
connection.setSSLSocketFactory(sslSocketFactory); connection.setSSLSocketFactory(sslSocketFactory);
} }
connection.setRequestMethod("GET"); connection.setRequestMethod("GET");
...@@ -143,18 +178,21 @@ public class HttpsUtil { ...@@ -143,18 +178,21 @@ public class HttpsUtil {
} }
} }
public static String post(String requestUrl, Map<String, Object> parameters, Map<String, String> headers, SSLSocketFactory sslSocketFactory, Proxy proxy) throws HttpRequestException { public static String post(String requestUrl, Map<String, Object> parameters, Map<String, String> headers,
System.out.println("----"+HttpUtil.toQueryString(parameters)); SSLSocketFactory sslSocketFactory, Proxy proxy) throws HttpRequestException {
System.out.println("----" + HttpUtil.toQueryString(parameters));
return HttpsUtil.post(requestUrl, HttpUtil.toQueryString(parameters), headers, sslSocketFactory, proxy); return HttpsUtil.post(requestUrl, HttpUtil.toQueryString(parameters), headers, sslSocketFactory, proxy);
} }
public static String post(String requestUrl, String text, Map<String, String> headers, SSLSocketFactory sslSocketFactory, Proxy proxy) throws HttpRequestException { public static String post(String requestUrl, String text, Map<String, String> headers,
SSLSocketFactory sslSocketFactory, Proxy proxy) throws HttpRequestException {
HttpsURLConnection connection = null; HttpsURLConnection connection = null;
try { try {
byte[] bytes = text == null ? new byte[0] : text.getBytes(); byte[] bytes = text == null ? new byte[0] : text.getBytes();
URL POST_URL = new URL(requestUrl); URL POST_URL = new URL(requestUrl);
connection = (HttpsURLConnection) (proxy == null ? POST_URL.openConnection() : POST_URL.openConnection(proxy)); connection = (HttpsURLConnection) (proxy == null ? POST_URL.openConnection()
if(sslSocketFactory != null) { : POST_URL.openConnection(proxy));
if (sslSocketFactory != null) {
connection.setSSLSocketFactory(sslSocketFactory); connection.setSSLSocketFactory(sslSocketFactory);
} }
...@@ -196,7 +234,7 @@ public class HttpsUtil { ...@@ -196,7 +234,7 @@ public class HttpsUtil {
* @return * @return
*/ */
public static String doPostForXml(String url, String xml) { public static String doPostForXml(String url, String xml) {
CloseableHttpClient httpClient = HttpClients.createDefault(); CloseableHttpClient httpClient = trustAll(); //HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url); HttpPost httpPost = new HttpPost(url);
httpPost.setConfig(requestConfig); httpPost.setConfig(requestConfig);
CloseableHttpResponse response = null; CloseableHttpResponse response = null;
...@@ -247,24 +285,20 @@ public class HttpsUtil { ...@@ -247,24 +285,20 @@ public class HttpsUtil {
*/ */
public static String doPostForJson(String url, String json) { public static String doPostForJson(String url, String json) {
Map<String, String> map = new HashMap<String, String>(); Map<String, String> map = new HashMap<String, String>();
String result = HttpUtil.post(url, json, map,null); String result = HttpUtil.post(url, json, map, null);
return result; return result;
} }
public static String doPostForJson(String url, String json,Map<String,String> headers) { public static String doPostForJson(String url, String json, Map<String, String> headers) {
String result = HttpUtil.post(url, json, headers,null); String result = HttpUtil.post(url, json, headers, null);
return result; return result;
} }
/** /**
* 模拟form表单的形式 ,上传文件 以输出流的形式把文件写入到url中,然后用输入流来获取url的响应 * 模拟form表单的形式 ,上传文件 以输出流的形式把文件写入到url中,然后用输入流来获取url的响应
* *
* @param url * @param url 请求地址 form表单url地址
* 请求地址 form表单url地址 * @param filePath 文件在服务器保存路径
* @param filePath
* 文件在服务器保存路径
* @return String url的响应信息返回值 * @return String url的响应信息返回值
* @throws IOException * @throws IOException
*/ */
...@@ -364,10 +398,8 @@ public class HttpsUtil { ...@@ -364,10 +398,8 @@ public class HttpsUtil {
/** /**
* 调用证书进行退款 WX提供 * 调用证书进行退款 WX提供
* *
* @param servicePartner * @param servicePartner 商户号
* 商户号 * @param sslPath 证书地址 "D:/10016225.p12"
* @param sslPath
* 证书地址 "D:/10016225.p12"
* @throws Exception * @throws Exception
*/ */
public static String doPostBySSL(String servicePartner, String sslPath, String xml, String requestUrl) public static String doPostBySSL(String servicePartner, String sslPath, String xml, String requestUrl)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment