java键对值SHA256加密接口请求

微信扫一扫,分享到朋友圈

java键对值SHA256加密接口请求
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.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.Map;
public class order {
private static String create_order(String ip, String merchantId, String key, String payWay, String amount)
throws NoSuchAlgorithmException, InvalidKeyException, IOException {
// url参数
Map<String, Object> url_map = new TreeMap<String, Object>();
url_map.put("merchantId", merchantId);
url_map.put("signatureMethod", "HmacSHA256");
url_map.put("signatureVersion", "1");
url_map.put("timestamp", System.currentTimeMillis());
// url参数拼接
StringBuilder url_data = new StringBuilder();
for (Map.Entry<String, Object> url :url_map.entrySet())  {
url_data.append(url.getKey()).append("=").append(url.getValue()).append("&");
}
url_data = new StringBuilder(url_data.substring(0, url_data.length() - 1));
// System.out.println(url_data);
// body参数
Map<String, Object> body_map = new TreeMap<String, Object>();
body_map.put("jUserId", "666666");
body_map.put("jUserIp", "110.110.110.110");
body_map.put("jOrderId", Long.toString(System.currentTimeMillis()));
body_map.put("orderType", "1");
body_map.put("payWay", payWay);
body_map.put("amount", amount);
body_map.put("currency", "CNY");
body_map.put("jUserName", "奥里给");
body_map.put("notifyUrl", "https://test.com");
// 全部参数
Map<String, Object> all_map = new TreeMap<String, Object>();
all_map.putAll(url_map);
all_map.putAll(body_map);
// 参数拼接
StringBuilder all_data = new StringBuilder();
for (Map.Entry<String, Object> url :all_map.entrySet())  {
all_data.append(url.getKey()).append("=").append(url.getValue()).append("&");
}
all_data = new StringBuilder(all_data.substring(0, all_data.length() - 1));
//  System.out.println("data:" + all_data);
// HmacSHA256加密
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "HmacSHA256");
sha256_HMAC.init(secret_key);
byte[] array = sha256_HMAC.doFinal(all_data.toString().getBytes(StandardCharsets.UTF_8));
StringBuilder sec = new StringBuilder();
for (byte item : array) {
sec.append(Integer.toHexString((item & 0xFF) | 0x100), 1, 3);
}
String signature_data = sec.toString().toUpperCase();
System.out.println("signature:" + signature_data);
String URL = ip + "?" + url_data + "&signature=" + signature_data;
System.out.println("url:" + URL);
HttpPost httppost = new HttpPost(URL);
// 请求body参数拼接
StringBuilder list_data = new StringBuilder();
for (Map.Entry<String, Object> m :body_map.entrySet())  {
list_data.append(m.getKey()).append("=").append(m.getValue()).append("&");
}
list_data = new StringBuilder(list_data.substring(0, list_data.length() - 1));
System.out.println("data:" + list_data);
// 创建订单-发送请求
StringEntity entity = new StringEntity(list_data.toString(), "utf-8");
entity.setContentEncoding("UTF-8");
entity.setContentType("application/x-www-form-urlencoded");
httppost.setEntity(entity);
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(httppost);
String strResult = EntityUtils.toString(response.getEntity());
System.out.println("result:" + strResult + "\t");
httppost.releaseConnection();
return strResult;
}
public static void main(String[] args) throws Exception {
create_order("https://test.com", "merchantId",
"key", "pay", "666");
}
}

微信扫一扫,分享到朋友圈

java键对值SHA256加密接口请求

互联网下半场不可不理解的云原生

上一篇

从零入门 Serverless | SAE 场景下,应用流量的负载均衡及路由策略配置实践

下一篇

你也可能喜欢

java键对值SHA256加密接口请求

长按储存图像,分享给朋友