惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
博客园 - 聂微东
Vercel News
Vercel News
aimingoo的专栏
aimingoo的专栏
F
Fortinet All Blogs
Microsoft Security Blog
Microsoft Security Blog
MongoDB | Blog
MongoDB | Blog
B
Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
WordPress大学
WordPress大学
Apple Machine Learning Research
Apple Machine Learning Research
阮一峰的网络日志
阮一峰的网络日志
大猫的无限游戏
大猫的无限游戏
GbyAI
GbyAI
Martin Fowler
Martin Fowler
M
MIT News - Artificial intelligence
The GitHub Blog
The GitHub Blog
博客园_首页
博客园 - 叶小钗
腾讯CDC
G
Google Developers Blog
Blog — PlanetScale
Blog — PlanetScale
宝玉的分享
宝玉的分享
D
Docker

祈雨的笔记

安全多方计算MPC spark原理解析 kueue执行源码分析 spark on k8s执行源码分析 spark-operator源码解析 系统压测遇到的缓存击穿问题 我的世界PC与安卓联机 蚂蚁金服流量投放平台的AIG改造 G1大对象致Old区占用率高 日志打印导致接口响应率下跌分析 Groovy加载类导致OOM分析 ERROR日志打印导致CPU满载 记OceanBase死锁超时 应用发版期间服务响应超时 Ark Serverless初探 系统优化复盘一二三 The user specified as a definer does not exist Kong网关初探 API网关选型调研 CPU火焰图常用工具 配置中心选型调研 root操作Nginx导致用户组错误 基于Proxifier使用代理 FastJSON字段智能匹配踩坑 Nacos初探 记一次Nginx服务器CPU满荷载故障 基于券系统分库分表的思考 limit不参与SQL成本计算致索引失效 Linux常用性能监控命令 golang低版本http2偶现400
阿里云API网关调用示例
祈雨的笔记 · 2018-12-07 · via 祈雨的笔记
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
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.Map;
import java.util.TreeMap;
import java.util.UUID;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;

public class TestHttpClient {
private static final String HTTP_METHOD = "POST";
private static final String ACCEPT_VALUE = "application/json;charset=utf-8";
private static final String CONTENT_TYPE_VALUE = "application/json;charset=utf-8";
private static final String HmacSHA256 = "HmacSHA256";

public static void main(String[] args) throws Exception {
test();
}

private static void test() throws Exception {
String appKey = "";
String appSecret = "";
String host = "";
String url = "";
String body = "";


Date date = new Date();
String uuid = UUID.randomUUID().toString();
String sign = getSign(appKey, appSecret, date, url, body, uuid);

HttpClient httpClient = HttpClients.createDefault();
HttpPost post = new HttpPost(host + url);

post.setHeader("date", String.valueOf(date.getTime()));
post.setHeader("accept", ACCEPT_VALUE);
post.setHeader("content-type", CONTENT_TYPE_VALUE);
post.setHeader("x-ca-key", appKey);
post.setHeader("x-ca-signature", sign);
post.setHeader("x-ca-timestamp", String.valueOf(date.getTime()));
post.setHeader("x-ca-nonce", uuid);
post.setHeader("content-md5", getBaseMD5(body));
post.setHeader("x-ca-signature-headers", "x-ca-nonce,x-ca-timestamp,x-ca-key");

StringEntity entity = new StringEntity(body, StandardCharsets.UTF_8);
post.setEntity(entity);

System.out.println("entity:" + entity);

HttpResponse response = httpClient.execute(post);

System.out.println("response:" + response);
System.out.println("Content: " + IOUtils.toString(response.getEntity().getContent()));
}




private static String getSign(String appKey, String appSecret, Date date, String url, String body, String uuid) throws Exception {
String contentMD5 = getBaseMD5(body);
String formatDate = String.valueOf(date.getTime());
String headers = getSignHeaders(appKey, date, uuid);

StringBuilder builder = new StringBuilder();
builder.append(HTTP_METHOD).append("\n");
builder.append(ACCEPT_VALUE).append("\n");
builder.append(contentMD5).append("\n");
builder.append(CONTENT_TYPE_VALUE).append("\n");
builder.append(formatDate).append("\n");
builder.append(headers);
builder.append(url);

String stringToSign = builder.toString();

Mac hmacSha256 = Mac.getInstance(HmacSHA256);
byte[] keyBytes = appSecret.getBytes(StandardCharsets.UTF_8);
hmacSha256.init(new SecretKeySpec(keyBytes, 0, keyBytes.length, HmacSHA256));
byte[] value = hmacSha256.doFinal(stringToSign.getBytes(StandardCharsets.UTF_8));
return Base64.encodeBase64String(value);
}




private static String getBaseMD5(String body) {
byte[] md5Bytes = DigestUtils.md5(body.getBytes(StandardCharsets.UTF_8));
final Base64 base64 = new Base64();
return new String(base64.encode(md5Bytes));
}




private static String getSignHeaders(String appKey, Date date, String uuid) {
long timestamp = date.getTime();
Map<String, String> map = new TreeMap<>();
map.put("x-ca-key", appKey);
map.put("x-ca-nonce", uuid);
map.put("x-ca-timestamp", String.valueOf(timestamp));

StringBuilder builder = new StringBuilder();
for (String key : map.keySet()) {
String value = map.getOrDefault(key, "");
String header = String.format("%s:%s\n", key, value);
builder.append(header);
}
return builder.toString();
}
}