Commit a963444a authored by zhangyong's avatar zhangyong

1

parent ee594046
...@@ -28,6 +28,7 @@ public class DateUtil { ...@@ -28,6 +28,7 @@ public class DateUtil {
public static final String FMT_A_DATE_MILLISECOND = "yyyy/MM/dd HH:mm:ss.SSS"; public static final String FMT_A_DATE_MILLISECOND = "yyyy/MM/dd HH:mm:ss.SSS";
public static final String FMT_DATE_ISO = "yyyy-MM-ddTHH:mm:ss.SSSZ"; public static final String FMT_DATE_ISO = "yyyy-MM-ddTHH:mm:ss.SSSZ";
public static final String FMT_UTC_GMT = "EEE, dd MMM yyyy HH:mm:ss z"; public static final String FMT_UTC_GMT = "EEE, dd MMM yyyy HH:mm:ss z";
public static final String FMT_YYYYMMddHHMMSS = "yyyyMMddHHmmss";
public static final Long SECOND = 1000L; public static final Long SECOND = 1000L;
public static final Long MINUTE = 1000L*60; public static final Long MINUTE = 1000L*60;
...@@ -423,5 +424,7 @@ public class DateUtil { ...@@ -423,5 +424,7 @@ public class DateUtil {
c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天 c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
return c.getTime(); return c.getTime();
} }
public static String getTimeStamp() {
return String.valueOf(System.currentTimeMillis() / 1000);
}
} }
package com.egolm.common; package com.egolm.common;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
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.security.KeyStore;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.SSLSocketFactory;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig; 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.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.util.EntityUtils; import org.apache.http.util.EntityUtils;
import com.egolm.common.exception.HttpRequestException; import com.egolm.common.exception.HttpRequestException;
...@@ -160,4 +171,130 @@ public class HttpsUtil { ...@@ -160,4 +171,130 @@ public class HttpsUtil {
} }
} }
/**
*
* <p>
* Title: 发送XML的post请求
* </p>
* <p>
* Description:
* </p>
*
* @param url
* @param xml
* @return
*/
public static String doPostForXml(String url, String xml) {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
httpPost.setConfig(requestConfig);
CloseableHttpResponse response = null;
HttpEntity entity = null;
try {
httpPost.setEntity(new StringEntity(xml, Charset.forName("UTF-8")));
response = httpClient.execute(httpPost);
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();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 调用证书进行退款 WX提供
*
* @param servicePartner
* 商户号
* @param sslPath
* 证书地址 "D:/10016225.p12"
* @throws Exception
*/
public static String doPostBySSL(String servicePartner, String sslPath, String xml, String requestUrl)
throws Exception {
KeyStore keyStore = KeyStore.getInstance("PKCS12");
FileInputStream instream = new FileInputStream(new File(sslPath));
keyStore.load(instream, servicePartner.toCharArray());
SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, servicePartner.toCharArray()).build();
byte[] bytes = xml == null ? new byte[0] : xml.getBytes();
URL POST_URL = new URL(requestUrl);
HttpsURLConnection connection = (HttpsURLConnection) POST_URL.openConnection();
connection.setSSLSocketFactory(sslcontext.getSocketFactory());
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Accept-Charset", "utf-8");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", String.valueOf(bytes.length));
OutputStream out = connection.getOutputStream();
out.write(bytes);
out.close();
return responseBody(connection);
}
public static String responseBody(HttpURLConnection connection) throws Exception {
byte[] bytes;
try {
bytes = FileUtil.streamToBytes(connection.getInputStream());
} catch (IOException e) {
bytes = FileUtil.streamToBytes(connection.getErrorStream());
}
String strHtml = null;
Map<String, List<String>> responseHeaders = connection.getHeaderFields();
List<String> contentTypes = responseHeaders.get("Content-Type");
String responseCharsetName = null;
String contentType = (contentTypes != null && contentTypes.size() > 0) ? contentTypes.get(0) : "";
String[] typeArray = contentType.split(";");
for (String type : typeArray) {
if (type.startsWith("charset=")) {
responseCharsetName = type.split("=", 2)[1];
} else if (type.startsWith("encoding=")) {
responseCharsetName = type.split("=", 2)[1];
}
}
if (responseCharsetName == null || responseCharsetName.trim().length() == 0) {
strHtml = new String(bytes, "utf-8");
String regex = "charset=([^\"'>]+)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(strHtml);
while (matcher.find()) {
String metaCharsetName = matcher.group(1);
if (metaCharsetName != null && metaCharsetName.trim().length() > 0) {
responseCharsetName = metaCharsetName;
}
}
}
if (responseCharsetName == null || responseCharsetName.trim().length() == 0) {
return strHtml;
} else {
return new String(bytes, responseCharsetName);
}
}
} }
...@@ -6,6 +6,7 @@ import java.io.InputStream; ...@@ -6,6 +6,7 @@ import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.Reader; import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.io.Writer; import java.io.Writer;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
...@@ -1061,4 +1062,32 @@ public class StringUtil { ...@@ -1061,4 +1062,32 @@ public class StringUtil {
public static String getRandom(int num){ public static String getRandom(int num){
return String.valueOf(Math.random()).substring(2, 2+num); return String.valueOf(Math.random()).substring(2, 2+num);
} }
/**
* 获取随机字符串
* @return
*/
public static String getNonceStr() {
// 随机数
String currTime = DateUtil.format(new Date(), DateUtil.FMT_YYYYMMddHHMMSS);
// 8位日期
String strTime = currTime.substring(8, currTime.length());
// 四位随机数
String strRandom = getRandom(4) + "";
// 10位序列号,可以自行调整。
return strTime + strRandom;
}
public static final String inputStream2String(InputStream in)
throws UnsupportedEncodingException, IOException {
if (in == null)
return "";
StringBuffer out = new StringBuffer();
byte[] b = new byte[4096];
for (int n; (n = in.read(b)) != -1;) {
out.append(new String(b, 0, n, "UTF-8"));
}
return out.toString();
}
} }
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