All Downloads are FREE. Search and download functionalities are using the official Maven repository.

mtons.oauth2.utils.HttpKitExt Maven / Gradle / Ivy

/*
+--------------------------------------------------------------------------
|   Mtons [#RELEASE_VERSION#]
|   ========================================
|   Copyright (c) 2014, 2015 mtons. All Rights Reserved
|   http://www.mtons.com
+---------------------------------------------------------------------------
*/
package mtons.oauth2.utils;

import org.apache.commons.lang3.StringUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;

/**
 * https 请求 微信为https的请求
 * Created by langhsu on 2017/9/2.
 */
public class HttpKitExt {
    /**
     * 构造请求参数
     *
     * @param url    url地址
     * @param params 参数集合
     * @return String 构造完成的url
     */
    public static String initParams(String url, Map params) {
        if (null == params || params.isEmpty()) {
            return url;
        }
        StringBuilder sb = new StringBuilder(url);
        if (url.indexOf("?") == -1) {
            sb.append("?");
        }
        sb.append(map2Url(params));
        return sb.toString();
    }

    /**
     * map构造url
     *
     * @param paramToMap 参数集合
     * @return String 构造完成的url
     */
    public static String map2Url(Map paramToMap) {
        if (null == paramToMap || paramToMap.isEmpty()) {
            return null;
        }
        StringBuffer url = new StringBuffer();
        boolean isfist = true;
        for (Map.Entry entry : paramToMap.entrySet()) {
            if (isfist) {
                isfist = false;
            } else {
                url.append("&");
            }
            url.append(entry.getKey()).append("=");
            String value = entry.getValue();
            if (StringUtils.isNotBlank(value))
                try {
                    value = URLEncoder.encode(value, "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    throw new RuntimeException(e);
                }
            url.append(value);
        }
        return url.toString();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy