You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

227 lines
7.3 KiB

4 months ago
4 months ago
  1. package com.example.demo.Util;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6. import java.net.HttpURLConnection;
  7. import java.net.URL;
  8. import java.net.URLEncoder;
  9. import java.security.MessageDigest;
  10. import java.security.NoSuchAlgorithmException;
  11. import java.util.ArrayList;
  12. import java.util.Collections;
  13. import java.util.List;
  14. import java.util.Random;
  15. /**
  16. * @开发目的: 测试使用
  17. * @开发人员: 弘历研发部 刘志红
  18. * @开发时间: 2024-1-26下午1:42:18
  19. * @软件版本: V1.0
  20. */
  21. public class GoldTistV2 {
  22. private static String url = "http://hcm.rzfwq.com/hwhcnewA/";
  23. /**
  24. * <pre>
  25. * 发送不带参数的GET的HTTP请求
  26. * </pre>
  27. *
  28. * @param reqUrl HTTP请求URL
  29. * @return HTTP响应的字符串
  30. */
  31. public static String doGet(String reqUrl) {
  32. HttpURLConnection url_con = null;
  33. String responseContent = null;
  34. try {
  35. StringBuffer params = new StringBuffer();
  36. String queryUrl = reqUrl;
  37. int paramIndex = reqUrl.indexOf("?");
  38. if (paramIndex > 0) {
  39. queryUrl = reqUrl.substring(0, paramIndex);
  40. String parameters = reqUrl.substring(paramIndex + 1, reqUrl.length());
  41. String[] paramArray = parameters.split("&");
  42. for (int i = 0; i < paramArray.length; i++) {
  43. String string = paramArray[i];
  44. int index = string.indexOf("=");
  45. if (index > 0) {
  46. String parameter = string.substring(0, index);
  47. String value = string.substring(index + 1, string.length());
  48. params.append(parameter);
  49. params.append("=");
  50. params.append(URLEncoder.encode(value, "UTF-8"));
  51. params.append("&");
  52. }
  53. }
  54. params = params.deleteCharAt(params.length() - 1);
  55. }
  56. URL url = new URL(queryUrl);
  57. url_con = (HttpURLConnection) url.openConnection();
  58. url_con.setRequestMethod("GET");
  59. System.setProperty("sun.net.client.defaultConnectTimeout", String.valueOf(500000));// (单位:毫秒)jdk1.4换成这个,连接超时
  60. System.setProperty("sun.net.client.defaultReadTimeout", String.valueOf(500000)); // (单位:毫秒)jdk1.4换成这个,读操作超时
  61. // url_con.setConnectTimeout(5000);//(单位:毫秒)jdk
  62. // 1.5换成这个,连接超时
  63. // url_con.setReadTimeout(5000);//(单位:毫秒)jdk 1.5换成这个,读操作超时
  64. url_con.setDoOutput(true);
  65. byte[] b = params.toString().getBytes();
  66. url_con.getOutputStream().write(b, 0, b.length);
  67. url_con.getOutputStream().flush();
  68. url_con.getOutputStream().close();
  69. InputStream in = url_con.getInputStream();
  70. BufferedReader rd = new BufferedReader(new InputStreamReader(in, "UTF-8"));
  71. String tempLine = rd.readLine();
  72. StringBuffer temp = new StringBuffer();
  73. String crlf = System.getProperty("line.separator");
  74. while (tempLine != null) {
  75. temp.append(tempLine);
  76. temp.append(crlf);
  77. tempLine = rd.readLine();
  78. }
  79. responseContent = temp.toString();
  80. rd.close();
  81. in.close();
  82. }
  83. catch (IOException e) {
  84. e.printStackTrace();
  85. }finally {
  86. if (url_con != null) {
  87. url_con.disconnect();
  88. }
  89. }
  90. return responseContent;
  91. }
  92. /**
  93. * 生成 MD5 签名
  94. *
  95. * @param params 待签名的参数数组
  96. * @param secretKey 密钥
  97. * @return 签名的 MD5 哈希值若出现异常则返回 null
  98. */
  99. public static String generateSignature(String[] params, String secretKey) {
  100. // 将参数数组转换为列表,以便进行排序
  101. List<String> paramList = new ArrayList<String>();
  102. Collections.addAll(paramList, params);
  103. // 对参数列表进行排序
  104. Collections.sort(paramList);
  105. // 拼接排序后的参数
  106. StringBuilder paramString = new StringBuilder();
  107. for (String param : paramList) {
  108. paramString.append(param);
  109. }
  110. // 拼接密钥
  111. paramString.append(secretKey);
  112. try {
  113. // 获取 MD5 消息摘要实例
  114. MessageDigest md = MessageDigest.getInstance("MD5");
  115. // 计算摘要,明确指定字符编码为 UTF-8
  116. byte[] digest = md.digest(paramString.toString().getBytes("UTF-8"));
  117. // 将字节数组转换为十六进制字符串
  118. StringBuilder hexString = new StringBuilder();
  119. for (byte b : digest) {
  120. String hex = Integer.toHexString(0xFF & b);
  121. if (hex.length() == 1) {
  122. hexString.append('0');
  123. }
  124. hexString.append(hex);
  125. }
  126. return hexString.toString();
  127. } catch (NoSuchAlgorithmException e) {
  128. e.printStackTrace();
  129. return null;
  130. } catch (java.io.UnsupportedEncodingException e) {
  131. e.printStackTrace();
  132. return null;
  133. }
  134. }
  135. /**
  136. * @功能描述: 生成随机数
  137. * @开发人员: 弘历研发部 刘志红 2024-1-25下午5:45:36 创建
  138. * @参数介绍: @param len
  139. * @参数介绍: @return
  140. * @返回数据: String
  141. * @版本编号: V1.0
  142. */
  143. public static String RandomUid(int len){
  144. String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghjklmnpqrstuvwxyz23456789";
  145. StringBuilder result = new StringBuilder();
  146. Random rnd = new Random();
  147. int strlen = characters.length();
  148. while (result.length() < 8) { // length of the random string.
  149. int index = (int) (rnd.nextFloat() * strlen);
  150. result.append(characters.charAt(index));
  151. }
  152. return "JB"+result.toString();
  153. }
  154. /**
  155. * 类型说明
  156. *
  157. 55 免费金币 金币系统退商品
  158. 56 永久金币 金币系统退商品
  159. 57 任务金币 金币系统退商品
  160. 58 免费金币 金币系统退金币
  161. 59 任务金币 金币系统退金币
  162. * @功能描述: 金币平台更新客户金币
  163. * @开发人员: 弘历研发部 刘志红 2025-3-21下午1:58:13 创建
  164. * @参数介绍: @param jwcode 账号
  165. * @参数介绍: @param lx 类型
  166. * @参数介绍: @param jbs 金币数量
  167. * @参数介绍: @param remark 备注
  168. * @参数介绍: @param yjjb 永久金币
  169. * @参数介绍: @param czr 操作人
  170. * @参数介绍: @param goodsname 商品名称
  171. * @参数介绍: @return
  172. * @返回数据: String 返回状态1加成功2减成功其他失败 -5 金币不足 -6 类型错误 -7签名错误
  173. * @版本编号: V1.0
  174. */
  175. public static String addCoinNew(String jwcode, int lx, double jbs,
  176. String remark,double yjjb,String czr,String goodsname){
  177. //查错误使用
  178. String sjzfc = RandomUid(10);
  179. String resp = "";
  180. try {
  181. DESGB desjbkc = new DESGB("Jbxt.205");
  182. String sk = "jwcode="+jwcode+"&number="+jbs+"&uid="+sjzfc+"&remark="+
  183. remark+"&czr="+czr+"&yjjb="+yjjb+"&czpt=4&goodsname="+goodsname+"&type="+lx;
  184. System.out.println("签名前:"+sk);
  185. String sign = generateSignature(sk.split("&"),"222251821eba7efab6d48e388b8f6baa");
  186. sk = desjbkc.encrypt(new String(sk.getBytes(), "UTF-8"));
  187. System.out.println(url+"goldUpdate_sign.gold?app=4&sk="+sk+"&sign="+sign);
  188. resp = doGet(url+"goldUpdate_sign.gold?app=4&sk="+sk+"&sign="+sign);
  189. }catch (Exception e){
  190. System.out.println("加金币异常"+e.toString());
  191. }
  192. System.out.println(resp);
  193. return resp;
  194. }
  195. public static void main(String[] args) throws Exception {
  196. //调用demo
  197. addCoinNew("94226013",65,-2,"测试",-1,"黄其振","文章11");
  198. }
  199. }