第五步:增加签名结果到请求参数中,发送请求
注意:签名也要做特殊URL编码
- [backcolor=transparent]String[backcolor=transparent] [backcolor=transparent]Signature[backcolor=transparent] [backcolor=transparent]=[backcolor=transparent] specialUrlEncode[backcolor=transparent]([backcolor=transparent]sign[backcolor=transparent]);[backcolor=transparent]// aMfgrx8DLS7vLfpeR1c2rrKLr0Q%3D
最终完整的GET请求HTTP为:
- [backcolor=transparent]http[backcolor=transparent]:[backcolor=transparent]//dyvmsapi.aliyuncs.com/?Signature=aMfgrx8DLS7vLfpeR1c2rrKLr0Q%3D&AccessKeyId=testId&Action=SingleCallByTts&CalledNumber=13000000000&CalledShowNumber=057112345678&Format=XML&OutId=123&RegionId=cn-hangzhou&SignatureMethod=HMAC-SHA1&SignatureNonce=f7d2d4ef-6d5f-4da4-86ed-88e001a66abb&SignatureVersion=1.0&Timestamp=2017-09-28T14%3A31%3A56Z&TtsCode=TTS_0000000&TtsParam=%7B%22code%22%3A%221234%22%2C%22product%22%3A%22test%22%7D&Version=2017-05-25
三、附加完整的Java签名Demo代码
- [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]class[backcolor=transparent] [backcolor=transparent]VmsSignDemo[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]static[backcolor=transparent] [backcolor=transparent]void[backcolor=transparent] main[backcolor=transparent]([backcolor=transparent]String[backcolor=transparent][][backcolor=transparent] args[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]throws[backcolor=transparent] [backcolor=transparent]Exception[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]String[backcolor=transparent] accessKeyId [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]"LTAIF0IDDKoh1Ra0"[backcolor=transparent];
- [backcolor=transparent] [backcolor=transparent]String[backcolor=transparent] accessSecret [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]"b5uIoiwg0tCeNU2ZwcEfvYl0EmeR2u"[backcolor=transparent];
- [backcolor=transparent] java[backcolor=transparent].[backcolor=transparent]text[backcolor=transparent].[backcolor=transparent]SimpleDateFormat[backcolor=transparent] df [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] java[backcolor=transparent].[backcolor=transparent]text[backcolor=transparent].[backcolor=transparent]SimpleDateFormat[backcolor=transparent]([backcolor=transparent]"yyyy-MM-dd'T'HH:mm:ss'Z'"[backcolor=transparent]);
- [backcolor=transparent] df[backcolor=transparent].[backcolor=transparent]setTimeZone[backcolor=transparent]([backcolor=transparent]new[backcolor=transparent] java[backcolor=transparent].[backcolor=transparent]util[backcolor=transparent].[backcolor=transparent]SimpleTimeZone[backcolor=transparent]([backcolor=transparent]0[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"GMT"[backcolor=transparent]));[backcolor=transparent]// 这里一定要设置GMT时区
- [backcolor=transparent] java[backcolor=transparent].[backcolor=transparent]util[backcolor=transparent].[backcolor=transparent]Map[backcolor=transparent]<[backcolor=transparent]String[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]String[backcolor=transparent]>[backcolor=transparent] paras [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] java[backcolor=transparent].[backcolor=transparent]util[backcolor=transparent].[backcolor=transparent]HashMap[backcolor=transparent]<[backcolor=transparent]String[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]String[backcolor=transparent]>();
- [backcolor=transparent] [backcolor=transparent]// 1. 系统参数
- [backcolor=transparent] paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"SignatureMethod"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"HMAC-SHA1"[backcolor=transparent]);
- [backcolor=transparent] paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"SignatureNonce"[backcolor=transparent],[backcolor=transparent] java[backcolor=transparent].[backcolor=transparent]util[backcolor=transparent].[backcolor=transparent]UUID[backcolor=transparent].[backcolor=transparent]randomUUID[backcolor=transparent]().[backcolor=transparent]toString[backcolor=transparent]());
- [backcolor=transparent] paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"AccessKeyId"[backcolor=transparent],[backcolor=transparent] accessKeyId[backcolor=transparent]);
- [backcolor=transparent] paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"SignatureVersion"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"1.0"[backcolor=transparent]);
- [backcolor=transparent] paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"Timestamp"[backcolor=transparent],[backcolor=transparent] df[backcolor=transparent].[backcolor=transparent]format[backcolor=transparent]([backcolor=transparent]new[backcolor=transparent] java[backcolor=transparent].[backcolor=transparent]util[backcolor=transparent].[backcolor=transparent]Date[backcolor=transparent]()));
- [backcolor=transparent] paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"Format"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"XML"[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]// 2. 业务API参数
- [backcolor=transparent] paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"Action"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"SingleCallByTts"[backcolor=transparent]);
- [backcolor=transparent] paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"Version"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"2017-05-25"[backcolor=transparent]);
- [backcolor=transparent] paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"RegionId"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"cn-hangzhou"[backcolor=transparent]);
- [backcolor=transparent] paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"CalledShowNumber"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"057112345678"[backcolor=transparent]);
- [backcolor=transparent] paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"CalledNumber"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"13000000000"[backcolor=transparent]);
- [backcolor=transparent] paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"TtsParam"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"{\"code\":\"1234\",\"product\":\"test\"}"[backcolor=transparent]);
- [backcolor=transparent] paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"TtsCode"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"TTS_0000000"[backcolor=transparent]);
- [backcolor=transparent] paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"OutId"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"123"[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]// 3. 去除签名关键字Key
- [backcolor=transparent] [backcolor=transparent]if[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]paras[backcolor=transparent].[backcolor=transparent]containsKey[backcolor=transparent]([backcolor=transparent]"Signature"[backcolor=transparent]))
- [backcolor=transparent] paras[backcolor=transparent].[backcolor=transparent]remove[backcolor=transparent]([backcolor=transparent]"Signature"[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]// 4. 参数KEY排序
- [backcolor=transparent] java[backcolor=transparent].[backcolor=transparent]util[backcolor=transparent].[backcolor=transparent]TreeMap[backcolor=transparent]<[backcolor=transparent]String[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]String[backcolor=transparent]>[backcolor=transparent] sortParas [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] java[backcolor=transparent].[backcolor=transparent]util[backcolor=transparent].[backcolor=transparent]TreeMap[backcolor=transparent]<[backcolor=transparent]String[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]String[backcolor=transparent]>();
- [backcolor=transparent] sortParas[backcolor=transparent].[backcolor=transparent]putAll[backcolor=transparent]([backcolor=transparent]paras[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]// 5. 构造待签名的字符串
- [backcolor=transparent] java[backcolor=transparent].[backcolor=transparent]util[backcolor=transparent].[backcolor=transparent]Iterator[backcolor=transparent]<[backcolor=transparent]String[backcolor=transparent]>[backcolor=transparent] it [backcolor=transparent]=[backcolor=transparent] sortParas[backcolor=transparent].[backcolor=transparent]keySet[backcolor=transparent]().[backcolor=transparent]iterator[backcolor=transparent]();
- [backcolor=transparent] [backcolor=transparent]StringBuilder[backcolor=transparent] sortQueryStringTmp [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]StringBuilder[backcolor=transparent]();
- [backcolor=transparent] [backcolor=transparent]while[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]it[backcolor=transparent].[backcolor=transparent]hasNext[backcolor=transparent]())[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]String[backcolor=transparent] key [backcolor=transparent]=[backcolor=transparent] it[backcolor=transparent].[backcolor=transparent]next[backcolor=transparent]();
- [backcolor=transparent] sortQueryStringTmp[backcolor=transparent].[backcolor=transparent]append[backcolor=transparent]([backcolor=transparent]"&"[backcolor=transparent]).[backcolor=transparent]append[backcolor=transparent]([backcolor=transparent]specialUrlEncode[backcolor=transparent]([backcolor=transparent]key[backcolor=transparent])).[backcolor=transparent]append[backcolor=transparent]([backcolor=transparent]"="[backcolor=transparent]).[backcolor=transparent]append[backcolor=transparent]([backcolor=transparent]specialUrlEncode[backcolor=transparent]([backcolor=transparent]paras[backcolor=transparent].[backcolor=transparent]get[backcolor=transparent]([backcolor=transparent]key[backcolor=transparent])));
- [backcolor=transparent] [backcolor=transparent]}
- [backcolor=transparent] [backcolor=transparent]String[backcolor=transparent] sortedQueryString [backcolor=transparent]=[backcolor=transparent] sortQueryStringTmp[backcolor=transparent].[backcolor=transparent]substring[backcolor=transparent]([backcolor=transparent]1[backcolor=transparent]);[backcolor=transparent]// 去除第一个多余的&符号
- [backcolor=transparent] [backcolor=transparent]StringBuilder[backcolor=transparent] stringToSign [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]StringBuilder[backcolor=transparent]();
- [backcolor=transparent] stringToSign[backcolor=transparent].[backcolor=transparent]append[backcolor=transparent]([backcolor=transparent]"GET"[backcolor=transparent]).[backcolor=transparent]append[backcolor=transparent]([backcolor=transparent]"&"[backcolor=transparent]);
- [backcolor=transparent] stringToSign[backcolor=transparent].[backcolor=transparent]append[backcolor=transparent]([backcolor=transparent]specialUrlEncode[backcolor=transparent]([backcolor=transparent]"/"[backcolor=transparent])).[backcolor=transparent]append[backcolor=transparent]([backcolor=transparent]"&"[backcolor=transparent]);
- [backcolor=transparent] stringToSign[backcolor=transparent].[backcolor=transparent]append[backcolor=transparent]([backcolor=transparent]specialUrlEncode[backcolor=transparent]([backcolor=transparent]sortedQueryString[backcolor=transparent]));
- [backcolor=transparent] [backcolor=transparent]String[backcolor=transparent] sign [backcolor=transparent]=[backcolor=transparent] sign[backcolor=transparent]([backcolor=transparent]accessSecret [backcolor=transparent]+[backcolor=transparent] [backcolor=transparent]"&"[backcolor=transparent],[backcolor=transparent] stringToSign[backcolor=transparent].[backcolor=transparent]toString[backcolor=transparent]());
- [backcolor=transparent] [backcolor=transparent]// 6. 签名最后也要做特殊URL编码
- [backcolor=transparent] [backcolor=transparent]String[backcolor=transparent] signature [backcolor=transparent]=[backcolor=transparent] specialUrlEncode[backcolor=transparent]([backcolor=transparent]sign[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]System[backcolor=transparent].[backcolor=transparent]out[backcolor=transparent].[backcolor=transparent]println[backcolor=transparent]([backcolor=transparent]paras[backcolor=transparent].[backcolor=transparent]get[backcolor=transparent]([backcolor=transparent]"SignatureNonce"[backcolor=transparent]));
- [backcolor=transparent] [backcolor=transparent]System[backcolor=transparent].[backcolor=transparent]out[backcolor=transparent].[backcolor=transparent]println[backcolor=transparent]([backcolor=transparent]"\r\n=========\r\n"[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]System[backcolor=transparent].[backcolor=transparent]out[backcolor=transparent].[backcolor=transparent]println[backcolor=transparent]([backcolor=transparent]paras[backcolor=transparent].[backcolor=transparent]get[backcolor=transparent]([backcolor=transparent]"Timestamp"[backcolor=transparent]));
- [backcolor=transparent] [backcolor=transparent]System[backcolor=transparent].[backcolor=transparent]out[backcolor=transparent].[backcolor=transparent]println[backcolor=transparent]([backcolor=transparent]"\r\n=========\r\n"[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]System[backcolor=transparent].[backcolor=transparent]out[backcolor=transparent].[backcolor=transparent]println[backcolor=transparent]([backcolor=transparent]sortedQueryString[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]System[backcolor=transparent].[backcolor=transparent]out[backcolor=transparent].[backcolor=transparent]println[backcolor=transparent]([backcolor=transparent]"\r\n=========\r\n"[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]System[backcolor=transparent].[backcolor=transparent]out[backcolor=transparent].[backcolor=transparent]println[backcolor=transparent]([backcolor=transparent]stringToSign[backcolor=transparent].[backcolor=transparent]toString[backcolor=transparent]());
- [backcolor=transparent] [backcolor=transparent]System[backcolor=transparent].[backcolor=transparent]out[backcolor=transparent].[backcolor=transparent]println[backcolor=transparent]([backcolor=transparent]"\r\n=========\r\n"[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]System[backcolor=transparent].[backcolor=transparent]out[backcolor=transparent].[backcolor=transparent]println[backcolor=transparent]([backcolor=transparent]sign[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]System[backcolor=transparent].[backcolor=transparent]out[backcolor=transparent].[backcolor=transparent]println[backcolor=transparent]([backcolor=transparent]"\r\n=========\r\n"[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]System[backcolor=transparent].[backcolor=transparent]out[backcolor=transparent].[backcolor=transparent]println[backcolor=transparent]([backcolor=transparent]signature[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]System[backcolor=transparent].[backcolor=transparent]out[backcolor=transparent].[backcolor=transparent]println[backcolor=transparent]([backcolor=transparent]"\r\n=========\r\n"[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]// 最终打印出合法GET请求的URL
- [backcolor=transparent] [backcolor=transparent]System[backcolor=transparent].[backcolor=transparent]out[backcolor=transparent].[backcolor=transparent]println[backcolor=transparent]([backcolor=transparent]"http://dyvmsapi.aliyuncs.com/?Signature="[backcolor=transparent] [backcolor=transparent]+[backcolor=transparent] signature [backcolor=transparent]+[backcolor=transparent] sortQueryStringTmp[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]}
- [backcolor=transparent] [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]static[backcolor=transparent] [backcolor=transparent]String[backcolor=transparent] specialUrlEncode[backcolor=transparent]([backcolor=transparent]String[backcolor=transparent] value[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]throws[backcolor=transparent] [backcolor=transparent]Exception[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]return[backcolor=transparent] java[backcolor=transparent].[backcolor=transparent]net[backcolor=transparent].[backcolor=transparent]URLEncoder[backcolor=transparent].[backcolor=transparent]encode[backcolor=transparent]([backcolor=transparent]value[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"UTF-8"[backcolor=transparent]).[backcolor=transparent]replace[backcolor=transparent]([backcolor=transparent]"+"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"%20"[backcolor=transparent]).[backcolor=transparent]replace[backcolor=transparent]([backcolor=transparent]"*"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"%2A"[backcolor=transparent]).[backcolor=transparent]replace[backcolor=transparent]([backcolor=transparent]"%7E"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"~"[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]}
- [backcolor=transparent] [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]static[backcolor=transparent] [backcolor=transparent]String[backcolor=transparent] sign[backcolor=transparent]([backcolor=transparent]String[backcolor=transparent] accessSecret[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]String[backcolor=transparent] stringToSign[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]throws[backcolor=transparent] [backcolor=transparent]Exception[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] javax[backcolor=transparent].[backcolor=transparent]crypto[backcolor=transparent].[backcolor=transparent]Mac[backcolor=transparent] mac [backcolor=transparent]=[backcolor=transparent] javax[backcolor=transparent].[backcolor=transparent]crypto[backcolor=transparent].[backcolor=transparent]Mac[backcolor=transparent].[backcolor=transparent]getInstance[backcolor=transparent]([backcolor=transparent]"HmacSHA1"[backcolor=transparent]);
- [backcolor=transparent] mac[backcolor=transparent].[backcolor=transparent]init[backcolor=transparent]([backcolor=transparent]new[backcolor=transparent] javax[backcolor=transparent].[backcolor=transparent]crypto[backcolor=transparent].[backcolor=transparent]spec[backcolor=transparent].[backcolor=transparent]SecretKeySpec[backcolor=transparent]([backcolor=transparent]accessSecret[backcolor=transparent].[backcolor=transparent]getBytes[backcolor=transparent]([backcolor=transparent]"UTF-8"[backcolor=transparent]),[backcolor=transparent] [backcolor=transparent]"HmacSHA1"[backcolor=transparent]));
- [backcolor=transparent] [backcolor=transparent]byte[backcolor=transparent][][backcolor=transparent] signData [backcolor=transparent]=[backcolor=transparent] mac[backcolor=transparent].[backcolor=transparent]doFinal[backcolor=transparent]([backcolor=transparent]stringToSign[backcolor=transparent].[backcolor=transparent]getBytes[backcolor=transparent]([backcolor=transparent]"UTF-8"[backcolor=transparent]));
- [backcolor=transparent] [backcolor=transparent]return[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] sun[backcolor=transparent].[backcolor=transparent]misc[backcolor=transparent].[backcolor=transparent]BASE64Encoder[backcolor=transparent]().[backcolor=transparent]encode[backcolor=transparent]([backcolor=transparent]signData[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]}
- [backcolor=transparent]}
- [backcolor=transparent]`