SignUtil.java 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package com.sqx.common.utils;
  2. import com.alibaba.fastjson.JSONObject;
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.apache.http.client.methods.HttpPost;
  5. import org.apache.http.entity.StringEntity;
  6. import org.apache.http.impl.client.CloseableHttpClient;
  7. import org.apache.http.impl.client.HttpClients;
  8. import org.apache.http.util.EntityUtils;
  9. import org.springframework.util.DigestUtils;
  10. import java.nio.charset.StandardCharsets;
  11. @Slf4j
  12. public class SignUtil {
  13. //获取快跑者平台签名
  14. public static String getSign(String body, String dev_key, String team_token, String ticket, Long timestamp, Integer version) {
  15. String message = "body=" + body + "&dev_key=" + dev_key + "&team_token=" + team_token + "&ticket=" + ticket + "&timestamp=" + timestamp + "&version=" + version + "WNEL248ZY8381W91EB66ZTULHBU176M9";
  16. String sign = encryptToMD5(message);
  17. log.info("sign = " + sign);
  18. return sign;
  19. }
  20. public static String encryptToMD5(String str) {
  21. return DigestUtils.md5DigestAsHex(str.getBytes());
  22. }
  23. public static void main(String[] args) {
  24. String sign = getSign("{ \"shop_id\": 147, \"shop_name\": \"创海测试店铺\", \"shop_tel\": \"15079248859\", \"shop_address\": \"江西省南昌市新建区黄家湖东路111号\", \"shop_tag\": \"115.827705,28.707832\", \"customer_name\": \"刘大人\", \"customer_tel\": \"15779631234\", \"customer_address\": \"新建区南昌交通学院(黄家湖校区)\", \"customer_tag\": \"115.827614,28.70773\", \"order_no\": \"202508150910421538\", \"pay_status\": 1 }", "31RRHA4O165VFN9W2DAPDYDH8N83BT12", "Q444DMV4LT8WSGRW", "05ffa0fe-2cb9-4350-86be-c2e625f43d3c", 1755504603L, 1);
  25. System.out.println("sign = " + sign);
  26. // JSONObject jsonObject = new JSONObject();
  27. // jsonObject.put("name","三三");
  28. // jsonObject.put("name2","三三2");
  29. // String s = jsonObject.toString();
  30. // System.out.println("s = " + s);
  31. // long currentTimeMillis = System.currentTimeMillis();
  32. // long currentTimeSeconds = currentTimeMillis / 1000;
  33. // System.out.println("当前时间的秒级时间戳: " + currentTimeSeconds);
  34. }
  35. public void http() {
  36. try (CloseableHttpClient client = HttpClients.createDefault()) {
  37. HttpPost httpPost = new HttpPost("http://example.com/api");
  38. String params = "key1=value1&key2=value2"; // 注意:这里不需要手动编码,HttpClient会处理它。
  39. StringEntity entity = new StringEntity(params, StandardCharsets.UTF_8);
  40. entity.setContentType("application/x-www-form-urlencoded"); // 设置Content-Type为application/x-www-form-urlencoded,但不是必须的,HttpClient会自动设置。
  41. httpPost.setEntity(entity);
  42. String responseBody = EntityUtils.toString(client.execute(httpPost).getEntity()); // 处理响应...
  43. System.out.println(responseBody);
  44. } catch (Exception e) {
  45. e.printStackTrace();
  46. }
  47. }
  48. }