package com.example.demo.Util; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Random; /** * @开发目的: 测试使用 * @开发人员: 弘历研发部 刘志红 * @开发时间: 2024-1-26下午1:42:18 * @软件版本: V1.0 */ public class GoldTistV2 { private static String url = "http://hcm.rzfwq.com/hwhcnewA/"; /** *
* 发送不带参数的GET的HTTP请求 ** * @param reqUrl HTTP请求URL * @return HTTP响应的字符串 */ public static String doGet(String reqUrl) { HttpURLConnection url_con = null; String responseContent = null; try { StringBuffer params = new StringBuffer(); String queryUrl = reqUrl; int paramIndex = reqUrl.indexOf("?"); if (paramIndex > 0) { queryUrl = reqUrl.substring(0, paramIndex); String parameters = reqUrl.substring(paramIndex + 1, reqUrl.length()); String[] paramArray = parameters.split("&"); for (int i = 0; i < paramArray.length; i++) { String string = paramArray[i]; int index = string.indexOf("="); if (index > 0) { String parameter = string.substring(0, index); String value = string.substring(index + 1, string.length()); params.append(parameter); params.append("="); params.append(URLEncoder.encode(value, "UTF-8")); params.append("&"); } } params = params.deleteCharAt(params.length() - 1); } URL url = new URL(queryUrl); url_con = (HttpURLConnection) url.openConnection(); url_con.setRequestMethod("GET"); System.setProperty("sun.net.client.defaultConnectTimeout", String.valueOf(500000));// (单位:毫秒)jdk1.4换成这个,连接超时 System.setProperty("sun.net.client.defaultReadTimeout", String.valueOf(500000)); // (单位:毫秒)jdk1.4换成这个,读操作超时 // url_con.setConnectTimeout(5000);//(单位:毫秒)jdk // 1.5换成这个,连接超时 // url_con.setReadTimeout(5000);//(单位:毫秒)jdk 1.5换成这个,读操作超时 url_con.setDoOutput(true); byte[] b = params.toString().getBytes(); url_con.getOutputStream().write(b, 0, b.length); url_con.getOutputStream().flush(); url_con.getOutputStream().close(); InputStream in = url_con.getInputStream(); BufferedReader rd = new BufferedReader(new InputStreamReader(in, "UTF-8")); String tempLine = rd.readLine(); StringBuffer temp = new StringBuffer(); String crlf = System.getProperty("line.separator"); while (tempLine != null) { temp.append(tempLine); temp.append(crlf); tempLine = rd.readLine(); } responseContent = temp.toString(); rd.close(); in.close(); } catch (IOException e) { e.printStackTrace(); }finally { if (url_con != null) { url_con.disconnect(); } } return responseContent; } /** * 生成 MD5 签名 * * @param params 待签名的参数数组 * @param secretKey 密钥 * @return 签名的 MD5 哈希值,若出现异常则返回 null */ public static String generateSignature(String[] params, String secretKey) { // 将参数数组转换为列表,以便进行排序 List