package com.boqu.qnweb.util; import lombok.Getter; import lombok.Setter; import org.apache.commons.lang3.StringUtils; import org.apache.http.HttpEntity; 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.params.CoreConnectionPNames; import org.apache.http.util.EntityUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import java.io.IOException; import java.util.*; /** * @author kindess * @description 联通业务util */ @Getter @Setter @Component @ConfigurationProperties public class UnicomUtil { /** * 联通分配appKey */ @Value("${unicom.appKey}") private String appKey; /** * 联通分配密钥privateKey */ @Value("${unicom.privateKey}") private String privateKey; /** * 充话费URL */ @Value("${unicom.phoneRechargeUrl}") private String phoneRechargeUrl; /** * 充流量URL */ @Value("${unicom.flowRechargeUrl}") private String flowRechargeUrl; /** * 生成签名 * * @param parameters * @param privateKey * @return * @throws Exception */ public static synchronized String createSign(Map<String, String> parameters, String privateKey) throws Exception { StringBuffer sb = new StringBuffer(); // 将参数以参数名的字典升序排序 SortedMap<String, String> sortParams = new TreeMap<String, String>(parameters); // 遍历排序的字典,并拼接"keyvalue"格式 for (Map.Entry<String, String> entry : sortParams.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); if (StringUtils.isNotEmpty(value)) { sb.append(key).append(value); } } //key 密钥值追加到参数最后 sb.append("key=").append(privateKey); String signValue = WXPayUtil.MD5(sb.toString()).toUpperCase(); return signValue; } /** * 接联通业务用的http请求 * * @param url 请求路径 * @param param 参数 * @return * @throws IOException */ public static synchronized String doHttpPost(String url, String param) throws IOException { String result = ""; HttpClient httpClient = new DefaultHttpClient(); // 请求超时 httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30000); // 读取超时 httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 60000); HttpPost httpPost = new HttpPost(url); httpPost.setHeader("Content-Type", "application/json"); if (param != null) { // 解决中文乱码问题 StringEntity entity = new StringEntity(param.toString(), "utf-8"); httpPost.setEntity(entity); } HttpResponse response = httpClient.execute(httpPost); if (response != null) { HttpEntity resEntity = response.getEntity(); if (resEntity != null) { result = EntityUtils.toString(resEntity, "UTF-8"); } } return result; } }
MD5工具类
/** * MD5工具类 * * @author : kindess **/ public class WXPayUtil { /** * 生成 MD5 * * @param data 待处理数据 * @return * @throws Exception */ public static String MD5(String data) throws Exception { MessageDigest md = MessageDigest.getInstance("MD5"); byte[] array = md.digest(data.getBytes("UTF-8")); StringBuilder sb = new StringBuilder(); for (byte item : array) { sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3)); } return sb.toString(); } }