1 Star 0 Fork 0

小圣/加油平台Demo示例

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
EcoTest.java 3.65 KB
一键复制 编辑 原始数据 按行查看 历史
小圣 提交于 2020-05-14 13:48 . 添加Demo代码
package com.ne.ice.eco;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;
import java.util.stream.Collectors;
public class EcoTest {
private static final CloseableHttpClient httpClient = HttpClients.createDefault();
public static void main(String[] args) throws Exception {
String url = "https://{domainHost}/eco-refuel-service/v1/refuel/station/list";
String appKey = "xxxxxxx";
String secret = "xxxxxxx";
long timestamp = System.currentTimeMillis();
// header
Map<String, String> headers = new HashMap<>();
// 请求参数
Map<String, Object> reqData = new TreeMap<>();
// 签名所需参数
Map<String, Object> signMap = new TreeMap<>();
reqData.put("lon", "104.071362");
reqData.put("lat", "30.556128");
reqData.put("ranger", "10");
// 时间戳和appkey以及下面的secretKey参与签名不属于请求参数
signMap.put("lon", "104.071362");
signMap.put("ranger", "10");
signMap.put("lat", "30.556128");
signMap.put("timestamp", timestamp);
signMap.put("appKey", appKey);
// 签名
String params = signMap.entrySet()
.stream()
// 过滤非空参数
.filter(item -> {
Object value = item.getValue();
if (value instanceof String) {
return null != value && !Objects.equals("", value);
}
return null != value;
})
.map(entity -> String.format("%s=%s", entity.getKey(), entity.getValue()))
.collect(Collectors.joining("&"));
params = String.format("%s%s", params, "&secretKey=" + secret).toLowerCase();
byte[] bytes = URLEncoder.encode(params, "UTF-8").getBytes("UTF-8");
String sign = sha256Digest(bytes);
headers.put("timestamp", timestamp + "");
headers.put("appKey", appKey);
headers.put("sign", sign);
String req = reqData.entrySet().stream()
.map(entity -> String.format("%s=%s", entity.getKey(), entity.getValue()))
.collect(Collectors.joining("&"));
String result = sendGet(url + "?" + req, headers);
System.out.println(result);
}
public static String sendGet(String url, Map<String, String> headers) {
HttpGet httpGet = new HttpGet(url);
try {
headers.forEach((k, v) -> {
httpGet.addHeader(k, v);
});
CloseableHttpResponse execute = httpClient.execute(httpGet);
return EntityUtils.toString(execute.getEntity());
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static String sha256Digest(byte[] content) throws Exception {
try {
MessageDigest md5 = MessageDigest.getInstance("SHA-256");
return toHexString(md5.digest(content));
} catch (Exception e) {
throw e;
}
}
public static String toHexString(byte[] digestBytes) {
StringBuilder hexStr = new StringBuilder();
for (byte b : digestBytes) {
hexStr.append(String.format("%02x", b & 0xff));
}
return hexStr.toString();
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/intersd/refule.git
git@gitee.com:intersd/refule.git
intersd
refule
加油平台Demo示例
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385