import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.math.BigInteger;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        try {
            String secret = "{{secret}}"; //用户秘钥
            String uid = "{{uid}}"; //用户OpenUid
            String method = "{{method}}"; //API方法名
            String timestamp = String.valueOf(System.currentTimeMillis() / 1000L); //时间戳
            //业务参数 {{p6}}
            Map<String, String> map = new HashMap<String, String>();
            map.put("uid", uid);
            map.put("method", method);
            map.put("timestamp", timestamp);
            //业务参数 {{p7}}
            List<String> keys = new ArrayList(map.keySet()); //SET转LIST
            Collections.sort(keys); //排序
            StringBuffer sb = new StringBuffer(secret); //拼接签名原串
            StringBuffer sb2 = new StringBuffer(); //拼接POST内容
            for (String key : keys) {
                sb.append(key + map.get(key));
                sb2.append(key + "=" + URLEncoder.encode(map.get(key), "UTF-8") + "&");
            }
            sb.append(secret);
            String sign = CalculateMd5(sb.toString()); //签名计算
            sb2.append("sign=" + sign); //添加签名参数
            String result = CallApi(sb2.toString()); //请求API
            System.out.println(result);
        } catch (Exception e) {
        }
    }

    private static String CalculateMd5(String content) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(content.getBytes());
            byte[] digest = md.digest();
            Formatter formatter = new Formatter();
            for (byte b : digest) {
                formatter.format("%02x", b);
            }
            String result = formatter.toString();
            return result;
        } catch (Exception e) {
        }
        return null;
    }

    private static String CallApi(String formData) {
        try {
            URL url = new URL("{{url}}");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            connection.setRequestProperty("Accept", "application/json");
            connection.setDoOutput(true);
            try (OutputStream os = connection.getOutputStream()) {
                byte[] input = formData.getBytes("utf-8");
                os.write(input, 0, input.length);
            }
            try (BufferedReader br = new BufferedReader(
                    new InputStreamReader(connection.getInputStream(), "utf-8"))) {
                StringBuilder response = new StringBuilder();
                String responseLine = null;
                while ((responseLine = br.readLine()) != null) {
                    response.append(responseLine.trim());
                }
                return response.toString();
            }
        } catch (Exception e) {
        }
        return null;
    }
}
