怎样取消联通java业务,java 对接联通业务util

此博客介绍了UnicomUtil组件,用于联通业务的API调用,包括appKey和privateKey的配置、创建签名的方法、以及执行HTTP POST请求的工具。重点展示了如何使用MD5算法生成签名,并处理中文字符编码问题。
摘要由CSDN通过智能技术生成

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(Mapparameters,

String privateKey) throws Exception {

StringBuffer sb = new StringBuffer();

// 将参数以参数名的字典升序排序

SortedMapsortParams = new TreeMap(parameters);

// 遍历排序的字典,并拼接"keyvalue"格式

for (Map.Entryentry : 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();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值