package org.treeleaf.common.http;

import org.apache.commons.lang3.StringUtils;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Map;

/**
 * Http请求父类
 * <p>
 * Created by yaoshuhong on 2015/6/29.
 */
public class Http extends HttpConnectionAttr {

    public static final String CONTENT_TYPE_FORM = "application/x-www-form-urlencoded";

    public static final String CONTENT_TYPE_JSON = "application/json";

    public static final String CONTENT_TYPE_XML = "text/xml";

    private static final String CHARSET = "UTF-8";

    /**
     * 根据是否采用ssl加密获取对应的HtppURLConnection实例
     *
     * @param url
     * @return
     * @throws Exception
     */
    protected HttpURLConnection getHttpURLConnection(URL url) throws Exception {
        if (this.isSsl()) {
            SSLContext sc = SSLContext.getInstance("TLSv1.2");
            sc.init(null, new TrustManager[]{new TrustAnyTrustManager()},
                    new java.security.SecureRandom());

            HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
            conn.setSSLSocketFactory(sc.getSocketFactory());
            conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
            return conn;
        }
        return (HttpURLConnection) url.openConnection();
    }

    /**
     * 将map参数使用'&'符号链接起来
     *
     * @param param 参数
     * @return
     */
    public static String param2UrlParam(Map<String, String> param) {

        if (param == null || param.isEmpty()) {
            return StringUtils.EMPTY;
        }

        StringBuilder stringBuilder = new StringBuilder();
        try {
            String s1 = "=";
            String s2 = "&";

            for (Map.Entry<String, String> entry : param.entrySet()) {
                if (entry.getValue() != null) {
                    stringBuilder.append(entry.getKey());
                    stringBuilder.append(s1);
                    stringBuilder.append(URLEncoder.encode(entry.getValue(), CHARSET));
                    stringBuilder.append(s2);
                }
            }

            if (stringBuilder.length() > 0) {
                stringBuilder.deleteCharAt(stringBuilder.length() - 1);
            }

            return stringBuilder.toString();

        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(String.format("参数中%s存在非法字符串,无法使用%s转义.", param.toString(), CHARSET), e);
        }
    }

}

@@ -445,24 +445,92 @@
 tionAttr %7B%0A%0A
+    public static final String NAME_CONTENT_TYPE = %22Content-Type%22;%0A%0A
     public s

修复Http缺少Content-Type常量的问题