import java.net.URLEncoder;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
/**
* 腾讯云公共请求参数
*/
public class RequestPublic {
public String getRequestPublic(String SQL_Action) {
//获取参数
String Action = SQL_Action;
String SecretId = SQL_SECRETID;
String Timestamp = String.valueOf(new Date().getTime()).substring(0, 10);
String Nonce = String.valueOf((int)(Math.random()*100000));
String Region = SQL_REGION;
String SignatureMethod = SQL_SIGNATUREMETHOD;
String cdbInstanceIds = SQL_CDBINSTANCEID;
String signURL = SQL_URL+"?";
//生成signURL TreeMap进行排序
Map<String ,String> reqMap = new TreeMap<String ,String>();
reqMap.put("Action", Action);
reqMap.put("SecretId", SecretId);
reqMap.put("Timestamp", Timestamp);
reqMap.put("Nonce", Nonce);
reqMap.put("Region", Region);
reqMap.put("SignatureMethod", SignatureMethod);
reqMap.put("cdbInstanceIds.0", cdbInstanceIds);
Set<String> keySet = reqMap.keySet();
Iterator<String> iter = keySet.iterator();
while (iter.hasNext()) {
String key = iter.next();
signURL += key+"="+reqMap.get(key)+"&";
}
signURL = signURL.substring(0 , signURL.length()-1);
//获得签名sig HMACSHA256加密并URL编码
HMACSHA256 hmacsha = new HMACSHA256();
try {
String sig = hmacsha.getSignature(signURL ,SQL_SECRET);
sig = URLEncoder.encode(sig,"UTF-8");
String getURL = signURL.substring(3)+"&Signature="+sig;
System.out.println(getURL);
return getURL;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "ERROR";
}
}
}