ScarlettYellow 2015-05-05
public static String sendNewData(String userName, String key,String url) { String result = ""; try { //把参数put到TreeMap里去 Map<String, String> map = new TreeMap<String, String>(); map.put("userName", userName); map.put("time", String.valueOf(System.currentTimeMillis() / 1000)); map.put("sign", buildSign(map, key));//构建签名,参数已经重新按照ASCII排序过 result = post(url, map);//post请求 System.out.println(result);// 打印返回的结果 } catch (Exception e) { e.printStackTrace(); } return ""; }
public static String buildSign(Map<String, String> map, String key) { Set<Entry<String, String>> set = map.entrySet(); StringBuffer sb = new StringBuffer(); //取出排序后的参数,逐一连接起来 for (Iterator<Map.Entry<String, String>> it = set.iterator(); it.hasNext();) { Map.Entry<String, String> me = it.next(); sb.append(me.getValue() + "|"); } sb.append(key); return sb.toString().toUpperCase();//返回最终排序后的结果,这里key不参与排序中,具体看接口规约 //return (Md5Encrypt.encrypt(sb.toString())).toUpperCase();//这个带有md5加密的,算法就不贴了,网上很多 }