生成一个接口调用的java代码 四个参数AccessKey ,sign,timestamp,nonce的参数位置是QUERY HEADER是hsPartyId body是json
时间: 2024-04-29 11:22:39 浏览: 117
下面是一个简单的示例代码,用于调用一个API,其中AccessKey、sign、timestamp和nonce作为查询参数,hsPartyId作为标头,JSON作为请求正文:
```
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
public class ApiCaller {
private static final String API_URL = "https://example.com/api";
private static final String ACCESS_KEY = "my_access_key";
private static final String SECRET_KEY = "my_secret_key";
private static final String HS_PARTY_ID = "my_hs_party_id";
public static void main(String[] args) throws Exception {
String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
String nonce = generateNonce();
// 构造查询参数
Map<String, String> queryParams = new HashMap<>();
queryParams.put("AccessKey", ACCESS_KEY);
queryParams.put("sign", generateSign(SECRET_KEY, timestamp, nonce));
queryParams.put("timestamp", timestamp);
queryParams.put("nonce", nonce);
// 构造标头
Map<String, String> headers = new HashMap<>();
headers.put("hsPartyId", HS_PARTY_ID);
// 构造请求正文
String requestBody = "{\"key\":\"value\"}";
// 发送请求
String response = sendGetRequest(API_URL, queryParams, headers, requestBody);
// 处理响应
System.out.println(response);
}
private static String sendGetRequest(String url, Map<String, String> queryParams, Map<String, String> headers, String requestBody) throws Exception {
StringBuilder sb = new StringBuilder(url);
if (!queryParams.isEmpty()) {
sb.append('?');
for (Map.Entry<String, String> entry : queryParams.entrySet()) {
sb.append(entry.getKey()).append('=').append(entry.getValue()).append('&');
}
sb.deleteCharAt(sb.length() - 1);
}
HttpURLConnection conn = (HttpURLConnection) new URL(sb.toString()).openConnection();
conn.setRequestMethod("GET");
for (Map.Entry<String, String> entry : headers.entrySet()) {
conn.setRequestProperty(entry.getKey(), entry.getValue());
}
if (requestBody != null && !requestBody.isEmpty()) {
conn.setDoOutput(true);
conn.getOutputStream().write(requestBody.getBytes());
}
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
conn.disconnect();
return response.toString();
}
private static String generateSign(String secretKey, String timestamp, String nonce) throws Exception {
String message = timestamp + nonce;
SecretKeySpec keySpec = new SecretKeySpec(secretKey.getBytes("UTF-8"), "HmacSHA256");
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(keySpec);
byte[] rawHmac = mac.doFinal(message.getBytes("UTF-8"));
return Base64.encodeBase64String(rawHmac);
}
private static String generateNonce() {
return String.valueOf(new Random().nextInt(1000000));
}
}
```
阅读全文