|
@@ -31,8 +31,156 @@ public class HttpUtil {
|
|
|
private static final String APPLICATION_FORM= "application/x-www-form-urlencoded;charset=utf-8";
|
|
|
|
|
|
private static final String APPLICATION_JSON = "application/json;charset=utf-8";
|
|
|
+
|
|
|
+ public static String actionPostForm(String uri, Map<String, String> heads, Map<String, Object> params) {
|
|
|
+ if(uri.toLowerCase().startsWith("https")) {
|
|
|
+ return httpsActionPostForm(uri, heads, params);
|
|
|
+ }else {
|
|
|
+ return httpActionPostForm(uri, heads, params);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ public static String httpActionPostForm(String uri, Map<String, String> heads, Map<String, Object> params) {
|
|
|
+ String result = null;
|
|
|
+ HttpURLConnection conn = null;
|
|
|
+ OutputStream os = null;
|
|
|
+ InputStream is = null;
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 获取链接
|
|
|
+ URL url = new URL(uri);
|
|
|
+ conn = (HttpURLConnection) url.openConnection();
|
|
|
+
|
|
|
+ conn.setRequestMethod("POST");
|
|
|
+ conn.setRequestProperty(CONTENT_TYPE, APPLICATION_JSON);
|
|
|
+ // 初始化
|
|
|
+ // 获取SSLSocketFactory对象
|
|
|
+
|
|
|
+ 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();
|
|
|
+ if (params != null) {
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ for (Map.Entry<String, Object> data : params.entrySet()) {
|
|
|
+ sb.append(data.getKey()).append("=").append(data.getValue()).append("&");
|
|
|
+ }
|
|
|
+ os = conn.getOutputStream();
|
|
|
+ os.write(sb.toString().getBytes());
|
|
|
+ os.flush();
|
|
|
+ }
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+ public static String httpsActionPostForm(String uri, Map<String, String> heads, Map<String, Object> params) {
|
|
|
+ String result = null;
|
|
|
+ HttpsURLConnection conn = null;
|
|
|
+ OutputStream os = null;
|
|
|
+ InputStream is = null;
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 获取链接
|
|
|
+ URL url = new URL(uri);
|
|
|
+ conn = (HttpsURLConnection) url.openConnection();
|
|
|
|
|
|
- public static String httpActionPost(String uri, Map<String, String> heads, Map<String, String> params) {
|
|
|
+ conn.setRequestMethod("POST");
|
|
|
+ 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();
|
|
|
+ if (params != null) {
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ for (Map.Entry<String, Object> data : params.entrySet()) {
|
|
|
+ sb.append(data.getKey()).append("=").append(data.getValue()).append("&");
|
|
|
+ }
|
|
|
+ os = conn.getOutputStream();
|
|
|
+ os.write(sb.toString().getBytes());
|
|
|
+ os.flush();
|
|
|
+ }
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+ public static String httpsActionPost(String uri, Map<String, String> heads, Map<String, String> params) {
|
|
|
String result = null;
|
|
|
HttpsURLConnection conn = null;
|
|
|
OutputStream os = null;
|
|
@@ -88,7 +236,7 @@ public class HttpUtil {
|
|
|
}
|
|
|
result = getResult(conn);
|
|
|
} catch (Exception e) {
|
|
|
- throw new StatusException("授权服务器访问失败", e);
|
|
|
+ throw new StatusException("服务器访问失败", e);
|
|
|
} finally {
|
|
|
try {
|
|
|
if (os != null) {
|
|
@@ -117,7 +265,7 @@ public class HttpUtil {
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
- public static String httpActionGet(String uri, Map<String, String> heads, Map<String, String> params) {
|
|
|
+ public static String httpsActionGet(String uri, Map<String, String> heads, Map<String, String> params) {
|
|
|
String result = null;
|
|
|
HttpsURLConnection conn = null;
|
|
|
OutputStream os = null;
|
|
@@ -176,7 +324,7 @@ public class HttpUtil {
|
|
|
|
|
|
result = getResult(conn);
|
|
|
} catch (Exception e) {
|
|
|
- throw new StatusException("授权服务器访问失败", e);
|
|
|
+ throw new StatusException("服务器访问失败", e);
|
|
|
} finally {
|
|
|
try {
|
|
|
if (os != null) {
|