|
@@ -0,0 +1,171 @@
|
|
|
+package cn.com.qmth.mps.util;
|
|
|
+
|
|
|
+import java.io.BufferedReader;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.InputStreamReader;
|
|
|
+import java.io.OutputStream;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.URL;
|
|
|
+import java.security.cert.CertificateException;
|
|
|
+import java.security.cert.X509Certificate;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import javax.net.ssl.HttpsURLConnection;
|
|
|
+import javax.net.ssl.SSLContext;
|
|
|
+import javax.net.ssl.SSLSocketFactory;
|
|
|
+import javax.net.ssl.TrustManager;
|
|
|
+import javax.net.ssl.X509TrustManager;
|
|
|
+
|
|
|
+import com.qmth.boot.core.exception.StatusException;
|
|
|
+
|
|
|
+public class HttpUtil {
|
|
|
+
|
|
|
+ /** 默认的编码格式 */
|
|
|
+ private static final String DEFAULT_CHARSET = "UTF-8";
|
|
|
+
|
|
|
+ private static final String CONTENT_TYPE = "Content-Type";
|
|
|
+
|
|
|
+ private static final String APPLICATION_JSON = "application/x-www-form-urlencoded;charset=utf-8";
|
|
|
+
|
|
|
+ private static final String METHOD_GET = "GET";
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @param params headers参数
|
|
|
+ * @param datas requestParams参数
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static String httpActionGet(String uri, Map<String, String> heads, Map<String, String> params) {
|
|
|
+ String result = null;
|
|
|
+ HttpsURLConnection conn = null;
|
|
|
+ OutputStream os = null;
|
|
|
+ InputStream is = null;
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 设置请求参数
|
|
|
+ if (params != null) {
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ for (Map.Entry<String, String> data : params.entrySet()) {
|
|
|
+ sb.append(data.getKey()).append("=").append(data.getValue()).append("&");
|
|
|
+ }
|
|
|
+ uri = uri+"?" + sb.toString();
|
|
|
+ }
|
|
|
+ // 获取链接
|
|
|
+ URL url = new URL(uri);
|
|
|
+ conn = (HttpsURLConnection) url.openConnection();
|
|
|
+
|
|
|
+ conn.setRequestMethod(METHOD_GET);
|
|
|
+ conn.setRequestProperty(CONTENT_TYPE, APPLICATION_JSON);
|
|
|
+ // ssl
|
|
|
+ SSLContext context = SSLContext.getInstance("SSL", "SunJSSE");
|
|
|
+ TrustManager[] tm = new TrustManager[] { new X509TrustManager() {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public X509Certificate[] getAcceptedIssuers() {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ } };
|
|
|
+ // 初始化
|
|
|
+ context.init(null, tm, new java.security.SecureRandom());
|
|
|
+ // 获取SSLSocketFactory对象
|
|
|
+ SSLSocketFactory ssf = context.getSocketFactory();
|
|
|
+ conn.setSSLSocketFactory(ssf);
|
|
|
+
|
|
|
+ conn.setUseCaches(false);
|
|
|
+ conn.setDoOutput(true);
|
|
|
+
|
|
|
+ // 设置额外的参数
|
|
|
+ if (heads != null && !heads.isEmpty()) {
|
|
|
+
|
|
|
+ for (Map.Entry<String, String> head : heads.entrySet()) {
|
|
|
+ conn.setRequestProperty(head.getKey(), head.getValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 创建链接
|
|
|
+ conn.connect();
|
|
|
+
|
|
|
+ result = getResult(conn);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new StatusException("授权服务器访问失败", e);
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ if (os != null) {
|
|
|
+ os.close();
|
|
|
+ os = null;
|
|
|
+ }
|
|
|
+ if (is != null) {
|
|
|
+ is.close();
|
|
|
+ is = null;
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ }
|
|
|
+
|
|
|
+ if (conn != null) {
|
|
|
+ conn.disconnect();
|
|
|
+ conn = null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得连接请求的返回数据
|
|
|
+ *
|
|
|
+ * @param conn
|
|
|
+ *
|
|
|
+ * @return 字符串
|
|
|
+ */
|
|
|
+ private static String getResult(HttpURLConnection conn) throws IOException {
|
|
|
+
|
|
|
+ StringBuilder text = new StringBuilder();
|
|
|
+
|
|
|
+ InputStream is = null;
|
|
|
+ InputStreamReader sr = null;
|
|
|
+ BufferedReader br = null;
|
|
|
+
|
|
|
+ int code = conn.getResponseCode();
|
|
|
+
|
|
|
+ try {
|
|
|
+ is = code != 200 ? conn.getErrorStream() : conn.getInputStream();
|
|
|
+
|
|
|
+ sr = new InputStreamReader(is, DEFAULT_CHARSET);
|
|
|
+ br = new BufferedReader(sr);
|
|
|
+
|
|
|
+ char[] chars = new char[4096];
|
|
|
+ int length = 0;
|
|
|
+
|
|
|
+ while ((length = br.read(chars)) != -1) {
|
|
|
+ text.append(chars, 0, length);
|
|
|
+ }
|
|
|
+ } finally {
|
|
|
+ if (br != null) {
|
|
|
+ br.close();
|
|
|
+ br = null;
|
|
|
+ }
|
|
|
+ if (sr != null) {
|
|
|
+ sr.close();
|
|
|
+ sr = null;
|
|
|
+ }
|
|
|
+ if (is != null) {
|
|
|
+ is.close();
|
|
|
+ is = null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (code != 200) {
|
|
|
+ throw new IOException(text.toString());
|
|
|
+ }
|
|
|
+ return text.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+}
|