代码拉取完成,页面将自动刷新
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();
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。