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

11

parent ec76a699
......@@ -12,7 +12,12 @@ import java.net.HttpURLConnection;
import java.net.Proxy;
import java.net.URL;
import java.nio.charset.Charset;
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.ArrayList;
import java.util.HashMap;
import java.util.List;
......@@ -29,15 +34,16 @@ 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.conn.ssl.NoopHostnameVerifier;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.ssl.TrustStrategy;
import org.apache.http.util.EntityUtils;
import com.egolm.common.exception.HttpRequestException;
/**
*
* @author 曲欣亮
......@@ -46,84 +52,113 @@ import com.egolm.common.exception.HttpRequestException;
*/
public class HttpsUtil {
private static RequestConfig requestConfig;
static {
RequestConfig.Builder configBuilder = RequestConfig.custom();
// 设置连接超时
configBuilder.setConnectTimeout(10*1000);
// 设置读取超时
configBuilder.setSocketTimeout(10*10000);
requestConfig = configBuilder.build();
}
/**
* 发送无参数的GET请求
*/
public static String doGet(String url) {
return doGet(url,null);
}
/**
* 发送有参数的GET请求,参数为MAP key-value格式
*/
public static String doGet(String url, Map<String, String> params) {
List<String> paramList=new ArrayList<String>();
if(params!=null){
for (String key : params.keySet()) {
paramList.add(key+"="+params.get(key));
}
if(paramList.size()>0){
if(url.indexOf("?")==-1){
url=url+"?"+StringUtil.join("&",paramList);
}else{
url=url+"&"+StringUtil.join("&",paramList);
}
}
}
System.out.println("doGet--"+url);
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url.replaceAll(" ", "%20"));
httpGet.setConfig(requestConfig);
CloseableHttpResponse response = null;
HttpEntity entity = null;
try {
response = httpclient.execute(httpGet);
entity = response.getEntity();
String result = EntityUtils.toString(entity, "UTF-8");
return result;
} catch (Exception e) {
throw new HttpRequestException(e);
} finally {
try {
if(entity!=null){
EntityUtils.consume(entity);
}
static {
RequestConfig.Builder configBuilder = RequestConfig.custom();
// 设置连接超时
configBuilder.setConnectTimeout(10 * 1000);
// 设置读取超时
configBuilder.setSocketTimeout(10 * 10000);
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请求
*/
public static String doGet(String url) {
return doGet(url, null);
}
/**
* 发送有参数的GET请求,参数为MAP key-value格式
*/
public static String doGet(String url, Map<String, String> params) {
List<String> paramList = new ArrayList<String>();
if (params != null) {
for (String key : params.keySet()) {
paramList.add(key + "=" + params.get(key));
}
if (paramList.size() > 0) {
if (url.indexOf("?") == -1) {
url = url + "?" + StringUtil.join("&", paramList);
} else {
url = url + "&" + StringUtil.join("&", paramList);
}
}
}
System.out.println("doGet--" + url);
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url.replaceAll(" ", "%20"));
httpGet.setConfig(requestConfig);
CloseableHttpResponse response = null;
HttpEntity entity = null;
try {
response = httpclient.execute(httpGet);
entity = response.getEntity();
String result = EntityUtils.toString(entity, "UTF-8");
return result;
} catch (Exception e) {
throw new HttpRequestException(e);
} finally {
try {
if (entity != null) {
EntityUtils.consume(entity);
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(response!=null){
response.close();
}
try {
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
httpclient.close();
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
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;
try {
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);
connection = (HttpsURLConnection) (proxy == null ? GET_URL.openConnection() : GET_URL.openConnection(proxy));
if(sslSocketFactory != null) {
connection = (HttpsURLConnection) (proxy == null ? GET_URL.openConnection()
: GET_URL.openConnection(proxy));
if (sslSocketFactory != null) {
connection.setSSLSocketFactory(sslSocketFactory);
}
connection.setRequestMethod("GET");
......@@ -142,22 +177,25 @@ public class HttpsUtil {
connection.disconnect();
}
}
public static String post(String requestUrl, Map<String, Object> parameters, Map<String, String> headers, SSLSocketFactory sslSocketFactory, Proxy proxy) throws HttpRequestException {
System.out.println("----"+HttpUtil.toQueryString(parameters));
public static String post(String requestUrl, Map<String, Object> parameters, Map<String, String> headers,
SSLSocketFactory sslSocketFactory, Proxy proxy) throws HttpRequestException {
System.out.println("----" + HttpUtil.toQueryString(parameters));
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;
try {
byte[] bytes = text == null ? new byte[0] : text.getBytes();
URL POST_URL = new URL(requestUrl);
connection = (HttpsURLConnection) (proxy == null ? POST_URL.openConnection() : POST_URL.openConnection(proxy));
if(sslSocketFactory != null) {
connection = (HttpsURLConnection) (proxy == null ? POST_URL.openConnection()
: POST_URL.openConnection(proxy));
if (sslSocketFactory != null) {
connection.setSSLSocketFactory(sslSocketFactory);
}
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
......@@ -181,7 +219,7 @@ public class HttpsUtil {
connection.disconnect();
}
}
/**
*
* <p>
......@@ -196,7 +234,7 @@ public class HttpsUtil {
* @return
*/
public static String doPostForXml(String url, String xml) {
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpClient httpClient = trustAll(); //HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
httpPost.setConfig(requestConfig);
CloseableHttpResponse response = null;
......@@ -236,35 +274,31 @@ public class HttpsUtil {
}
}
}
public static String doPost(String url) {
String result = HttpUtil.post(url);
return result;
}
/**
* 发送有参数的POST请求,参数为json格式
*/
public static String doPostForJson(String url, String json) {
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;
}
public static String doPostForJson(String url, String json,Map<String,String> headers) {
String result = HttpUtil.post(url, json, headers,null);
public static String doPostForJson(String url, String json, Map<String, String> headers) {
String result = HttpUtil.post(url, json, headers, null);
return result;
}
/**
* 模拟form表单的形式 ,上传文件 以输出流的形式把文件写入到url中,然后用输入流来获取url的响应
*
* @param url
* 请求地址 form表单url地址
* @param filePath
* 文件在服务器保存路径
* @param url 请求地址 form表单url地址
* @param filePath 文件在服务器保存路径
* @return String url的响应信息返回值
* @throws IOException
*/
......@@ -307,7 +341,7 @@ public class HttpsUtil {
sb.append("--"); // 必须多两道线
sb.append(BOUNDARY);
sb.append("\r\n");
sb.append("Content-Disposition: form-data;name=\"file\";filename=\"" + file.getName() + "\"\r\n");
sb.append("Content-Disposition: form-data;name=\"file\";filename=\"" + file.getName() + "\"\r\n");
sb.append("Content-Type:application/octet-stream\r\n\r\n");
byte[] head = sb.toString().getBytes("utf-8");
......@@ -357,17 +391,15 @@ public class HttpsUtil {
reader.close();
}
}
}
return result;
}
/**
* 调用证书进行退款 WX提供
*
* @param servicePartner
* 商户号
* @param sslPath
* 证书地址 "D:/10016225.p12"
* @param servicePartner 商户号
* @param sslPath 证书地址 "D:/10016225.p12"
* @throws Exception
*/
public static String doPostBySSL(String servicePartner, String sslPath, String xml, String requestUrl)
......@@ -393,7 +425,7 @@ public class HttpsUtil {
out.close();
return responseBody(connection);
}
public static String responseBody(HttpURLConnection connection) throws Exception {
byte[] bytes;
try {
......
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