使用云解析的添加域名时,参照官方的请求签名文档进行的排序和编码,但是在调用后返回的却是4100详细代码如下:
String strUrl = "POSTcns.api.qcloud.com/v2/index.php?Action=DomainCreate&Nonce=9572234&Region=gz&SecretId=AKIDQQaiqoMirwPj07NMzCjLw5MavV4TDs18&SignatureMethod=HmacSHA1&Timestamp=1540956598";
try {
//进行HmacSHA1编码
SecretKeySpec signKey = new SecretKeySpec(strSecretKey.getBytes("utf-8"),"HmacSHA1");
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(signKey);
byte[] rawHmac = mac.doFinal(strUrl.getBytes("utf-8"));
System.out.println(Hex.encodeHexString(rawHmac));
//64位编码后的Signature
String text = Base64.getEncoder().encodeToString(rawHmac);
System.out.println(text);
//url编码后的Signature
String urlText = java.net.URLEncoder.encode(text,"utf-8");
System.out.println(urlText);
//请求的url
String requestUrl = "https://cns.api.qcloud.com/v2/index.php";
HttpClient client = new HttpClient();
PostMethod post = new PostMethod(requestUrl);
//在头文件中设置转码
post.addRequestHeader("Content-Type", format);
//请求参数
NameValuePair[] data = {
new NameValuePair("Action","DomainCreate"),
new NameValuePair("SecretId","AKIDQQaiqoMirwPj07NMzCjLw5MavV4TDs18"),
new NameValuePair("Region","ap-guangzhou"),
new NameValuePair("Timestamp","1540956598"),
new NameValuePair("Nonce","9572234"),
new NameValuePair("Signature",urlText),
new NameValuePair("SignaMethod","HmacSHA1"),
new NameValuePair("domain","dev.test.com"),
new NameValuePair("projectId","0"),
};
post.setRequestBody(data);
client.executeMethod(post);
//打印返回头
Header[] headers = post.getResponseHeaders();
for (Header h : headers) {
System.out.println(h.toString());
}
//获取返回体
String result = new String(post.getResponseBodyAsString().getBytes("utf-8"));
System.out.println("result:" + result);
post.releaseConnection();
} catch (Exception e) {
e.printStackTrace();
}
请帮忙解答下,谢谢
相似问题