版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_24138151/article/details/80974664
本贴只做记录,感兴趣的朋友可以私信我或加我Q询问464744895
需求:根据电话号码,自动拨打电话TTS生成语音进行提示
用的是提供外呼技术支持的服务商,测试代码可用:
package weixin.dianhua;
import java.net.Socket;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import net.sf.json.JSONObject;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import java.io.*;
import Decoder.BASE64Encoder;
import java.util.TreeMap;
public class dmdemo_10256_test {
// public static void main(String[] args) throws Exception
// {
public void dianhua(String userstr,String tel)throws Exception{
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost("***.******.***", 80, "http");
//client.getHostConfiguration().setHost("114.55.234.215", 80, "http");
HttpMethod method = getPostMethod(userstr,tel);
client.executeMethod(method);
//System.out.println(method.getStatusLine());
/*
String response = new String(method.getResponseBodyAsString().getBytes("utf-8"));
System.out.println(response);
*/
/* getResponseBodyAsStream start */
InputStream inputStream = method.getResponseBodyAsStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
StringBuffer response = new StringBuffer();
String read = "";
while((read = br.readLine()) != null){
response.append(read);
}
System.out.println(response);
/* getResponseBodyAsStream start */
method.releaseConnection();
}
public static String encrypt(String strKey, String strIn) throws Exception {
String ivstr = strKey.substring(0, 16);
SecretKeySpec skeySpec = getKey(strKey);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
IvParameterSpec iv = new IvParameterSpec(ivstr.getBytes());
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
byte[] encrypted = cipher.doFinal(strIn.getBytes());
return new BASE64Encoder().encode(encrypted);
}
private static SecretKeySpec getKey(String strKey) throws Exception {
byte[] arrBTmp = strKey.getBytes();
byte[] arrB = new byte[16]; // 创建一个空的16位字节数组(默认值为0)
for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
arrB[i] = arrBTmp[i];
}
SecretKeySpec skeySpec = new SecretKeySpec(arrB, "AES");
return skeySpec;
}
private static HttpMethod getPostMethod(String userstr,String tel) throws Exception {
PostMethod post = new UTF8PostMethod("/api/v2/data"); // 数据请求接口
//PostMethod post = new UTF8PostMethod("/api/dm_1.php"); // 数据请求接口
int vccId = 200027;
int proId = 10356;
String token = "631ddb2c05e054303c318d06d2202420c2ee1691";
Map templateInfo = new LinkedHashMap();
//templateInfo.put("productPrice", "99.8");
//templateInfo.put("productName", "产品名称");
// String userstr = "今天下午三点开会";
templateInfo.put("remindMsg", userstr);
// LinkedHashMap 是HashMap的一个子类,如果需要输出的顺序和输入的相同
Map data = new LinkedHashMap(); // 顺序是完全随机,具有很快的访问速度
Map map = new TreeMap(); // 默认是按键值的升序排序
map.put("phoneNum", tel);
map.put("callInterval", 20);
map.put("callNum", 0);
map.put("templateInfo", templateInfo);
data.put("vccId", vccId);
data.put("proId", proId);
String mapStr = JSONObject.fromObject(map).toString();
String str = mapStr + token;
String sign = SHA1(str);
data.put("sign", sign);
//data.put("data", encrypt(token, mapStr));
data.put("data", map);
//MessageDigest md = MessageDigest.getInstance("SHA-1");
//md.reset();
JSONObject json = JSONObject.fromObject(data);
post.setRequestBody(json.toString());
System.out.println(json);
//System.exit(0);
return post;
}
public static String SHA1(String decript) {
try {
MessageDigest digest = java.security.MessageDigest.getInstance("SHA-1");
digest.update(decript.getBytes());
byte messageDigest[] = digest.digest();
// Create Hex String
StringBuffer hexString = new StringBuffer();
// 字节数组转换为 十六进制 数
for (int i = 0; i < messageDigest.length; i++) {
String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
if (shaHex.length() < 2) {
hexString.append(0);
}
hexString.append(shaHex);
}
return hexString.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return "";
}
/*
* 中文转unicode编码
*/
public static String gbEncoding(final String gbString) {
char[] utfBytes = gbString.toCharArray();
String unicodeBytes = "";
for (int i = 0; i < utfBytes.length; i++) {
String hexB = Integer.toHexString(utfBytes[i]);
if (hexB.length() <= 2) {
hexB = "00" + hexB;
}
unicodeBytes = unicodeBytes + "\\u" + hexB;
}
return unicodeBytes;
}
/*
* unicode编码转中文
*/
public static String decodeUnicode(final String dataStr) {
int start = 0;
int end = 0;
final StringBuffer buffer = new StringBuffer();
while (start > -1) {
end = dataStr.indexOf("\\u", start + 2);
String charStr = "";
if (end == -1) {
charStr = dataStr.substring(start + 2, dataStr.length());
} else {
charStr = dataStr.substring(start + 2, end);
}
char letter = (char) Integer.parseInt(charStr, 16); // 16进制parse整形字符串。
buffer.append(new Character(letter).toString());
start = end;
}
return buffer.toString();
}
}
UTF8工具
package weixin.dianhua;
import org.apache.commons.httpclient.methods.PostMethod;
public class UTF8PostMethod extends PostMethod{
public UTF8PostMethod(String url){
super(url);
}
@Override
public String getRequestCharSet() {
//return super.getRequestCharSet();
return "utf-8";
}
}