forked from QcloudApi/qcloud_sign_java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSignTest.java
More file actions
156 lines (132 loc) · 5.14 KB
/
SignTest.java
File metadata and controls
156 lines (132 loc) · 5.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*=============================================================================
#
# FileName: SignTest.java
# Desc: qcloud 签名测试工具
#
# Author: gavinyao
# Email: [email protected]
#
# Created: 2014-12-19 11:29:41
# Version: 0.0.1
# History:
# 0.0.1 | gavinyao | 2014-12-19 11:29:41 | initialization
#
=============================================================================*/
import java.net.*;
import java.io.*;
import java.util.Map;
import java.util.TreeMap;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.security.Timestamp;
import javax.net.ssl.*;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
public class SignTest {
// SecretId 和 SecretKey
private static final String SECRET_ID = "YOUR_SECRET_ID";
private static final String SECRET_KEY = "YOUR_SECRET_KEY";
public static void main(String[] args) {
TreeMap<String, Object> requestParams = new TreeMap<String, Object>();
requestParams.put("SecretId", SECRET_ID);
requestParams.put("Region", "gz");
requestParams.put("Timestamp", System.currentTimeMillis() / 1000);
Random rand = new Random();
requestParams.put("Nonce", rand.nextInt(java.lang.Integer.MAX_VALUE));
requestParams.put("Action", "DescribeInstances");
String requestMethod = "POST";
String requestHost = "cvm.api.qcloud.com";
String requestPath = "/v2/index.php";
try {
String plainText = QcloudSign.makeSignPlainText(requestParams, requestMethod, requestHost, requestPath);
String sign = QcloudSign.sign(plainText, SECRET_KEY);
System.out.println("原文: " + plainText);
System.out.println("签名: " + sign);
if (requestMethod.equals("GET")) {
requestParams.put("Signature", java.net.URLEncoder.encode(sign, "UTF-8"));
} else {
requestParams.put("Signature", sign);
}
String retStr = _sendRequest("https://" + requestHost + requestPath, requestParams, requestMethod);
System.out.println(retStr);
} catch(Exception e) {
System.out.println(e);
}
}
protected static String _sendRequest(String url,
Map<String, Object> requestParams, String requestMethod)
{
String result = "";
BufferedReader in = null;
String paramStr = "";
for(String key: requestParams.keySet()) {
if (!paramStr.isEmpty()) {
paramStr += '&';
}
// paramStr += key + '=' + requestParams.get(key).toString();
paramStr += key + '=' + URLEncoder.encode(requestParams.get(key).toString());
}
try {
if (requestMethod.equals("GET")) {
if (url.indexOf('?') > 0)
{
url += '&' + paramStr;
} else {
url += '?' + paramStr;
}
}
URL realUrl = new URL(url);
URLConnection connection = null;
if (url.substring(0, 5).toUpperCase().equals("HTTPS")) {
HttpsURLConnection httpsConn = (HttpsURLConnection)realUrl.openConnection();
httpsConn.setHostnameVerifier(new HostnameVerifier(){
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
connection = httpsConn;
} else {
connection = realUrl.openConnection();
}
// 设置通用的请求属性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
if (requestMethod.equals("POST")) {
// 发送POST请求必须设置如下两行
connection.setDoOutput(true);
connection.setDoInput(true);
// 获取URLConnection对象对应的输出流
PrintWriter out = new PrintWriter(connection.getOutputStream());
// 发送请求参数
out.print(paramStr);
// flush输出流的缓冲
out.flush();
}
// 建立实际的连接
connection.connect();
// 定义 BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 使用finally块来关闭输入流
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
}
}