package com.project.common.utils;
|
|
import java.io.BufferedReader;
|
import java.io.IOException;
|
import java.io.InputStream;
|
import java.io.InputStreamReader;
|
import java.io.OutputStream;
|
import java.io.OutputStreamWriter;
|
import java.net.HttpURLConnection;
|
import java.net.MalformedURLException;
|
import java.net.URL;
|
import java.security.MessageDigest;
|
import java.util.Map;
|
|
public class ZegoUtils
|
{
|
|
/**
|
* 生成签名
|
* @param appId 应用 AppId
|
* @param signatureNonce 签名随机码
|
* @param serverSecret 服务端API密钥
|
* @param timestamp 签名时间戳
|
* @return 生成的签名字符串
|
*/
|
public static String generateSignature(long appId, String signatureNonce, String serverSecret, long timestamp)
|
{
|
String str = String.valueOf(appId) + signatureNonce + serverSecret + String.valueOf(timestamp);
|
String signature = "";
|
try{
|
//创建一个提供信息摘要算法的对象,初始化为md5算法对象
|
MessageDigest md = MessageDigest.getInstance("MD5");
|
//计算后获得字节数组
|
byte[] bytes = md.digest(str.getBytes("utf-8"));
|
//把数组每一字节换成16进制连成md5字符串
|
signature = bytesToHex(bytes);
|
}catch (Exception e) {
|
e.printStackTrace();
|
}
|
return signature;
|
}
|
|
/**
|
* 字节数组转16进制
|
* @param bytes 需要转换的byte数组
|
* @return 转换后的Hex字符串
|
*/
|
public static String bytesToHex(byte[] bytes)
|
{
|
StringBuffer md5str = new StringBuffer();
|
//把数组每一字节换成16进制连成md5字符串
|
int digital;
|
for (int i = 0; i < bytes.length; i++) {
|
digital = bytes[i];
|
if (digital < 0) {
|
digital += 256;
|
}
|
if (digital < 16) {
|
md5str.append("0");
|
}
|
md5str.append(Integer.toHexString(digital));
|
}
|
return md5str.toString();
|
}
|
|
/**
|
* 发送HTTP-GET请求
|
* @param httpurl 请求路径
|
* @return 响应内容
|
*/
|
public static String sendGet(String httpurl)
|
{
|
HttpURLConnection connection = null;
|
InputStream is = null;
|
BufferedReader br = null;
|
String result = null;// 返回结果字符串
|
try {
|
// 创建远程url连接对象
|
URL url = new URL(httpurl);
|
// 通过远程url连接对象打开一个连接,强转成httpURLConnection类
|
connection = (HttpURLConnection) url.openConnection();
|
// 设置连接方式:get
|
connection.setRequestMethod("GET");
|
// 设置连接主机服务器的超时时间:15000毫秒
|
connection.setConnectTimeout(15000);
|
// 设置读取远程返回的数据时间:60000毫秒
|
connection.setReadTimeout(60000);
|
// 发送请求
|
connection.connect();
|
// 通过connection连接,获取输入流
|
if (connection.getResponseCode() == 200) {
|
is = connection.getInputStream();
|
// 封装输入流is,并指定字符集
|
br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
|
// 存放数据
|
StringBuffer sbf = new StringBuffer();
|
String temp = null;
|
while ((temp = br.readLine()) != null) {
|
sbf.append(temp);
|
sbf.append("\r\n");
|
}
|
result = sbf.toString();
|
}
|
} catch (MalformedURLException e) {
|
e.printStackTrace();
|
} catch (IOException e) {
|
e.printStackTrace();
|
} finally {
|
// 关闭资源
|
if (null != br) {
|
try {
|
br.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
if (null != is) {
|
try {
|
is.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
connection.disconnect();// 关闭远程连接
|
}
|
return result;
|
}
|
|
/**
|
* 发送HTTP-POST请求
|
* @param param 消息体参数
|
* @return 响应内容
|
*/
|
public static String sendPost(String httpUrl, String param)
|
{
|
HttpURLConnection connection = null;
|
InputStream is = null;
|
OutputStream os = null;
|
BufferedReader br = null;
|
String result = null;
|
try {
|
URL url = new URL(httpUrl);
|
// 通过远程url连接对象打开连接
|
connection = (HttpURLConnection) url.openConnection();
|
// 设置连接请求方式
|
connection.setRequestMethod("POST");
|
// 设置连接主机服务器超时时间:15000毫秒
|
connection.setConnectTimeout(15000);
|
// 设置读取主机服务器返回数据超时时间:60000毫秒
|
connection.setReadTimeout(60000);
|
// 默认值为:false,当向远程服务器传送数据/写数据时,需要设置为true
|
connection.setDoOutput(true);
|
// 默认值为:true,当前向远程服务读取数据时,设置为true,该参数可有可无
|
connection.setDoInput(true);
|
// 设置传入参数的格式:请求参数应该是 name1=value1&name2=value2 的形式。
|
connection.setRequestProperty("Content-Type", "application/json");
|
// 建立连接
|
connection.connect();
|
// 通过连接对象获取一个输出流对象
|
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(),"UTF-8");
|
writer.write(param);
|
writer.flush();
|
// 通过连接对象获取一个输入流,向远程读取
|
if (connection.getResponseCode() == 200) {
|
is = connection.getInputStream();
|
// 对输入流对象进行包装:charset根据工作项目组的要求来设置
|
br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
|
StringBuffer sbf = new StringBuffer();
|
String temp = null;
|
// 循环遍历一行一行读取数据
|
while ((temp = br.readLine()) != null) {
|
sbf.append(temp);
|
sbf.append("\r");
|
}
|
result = sbf.toString();
|
}
|
} catch (MalformedURLException e) {
|
e.printStackTrace();
|
} catch (IOException e) {
|
e.printStackTrace();
|
} finally {
|
// 关闭资源
|
if (null != br) {
|
try {
|
br.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
if (null != os) {
|
try {
|
os.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
if (null != is) {
|
try {
|
is.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
// 断开与远程地址url的连接
|
connection.disconnect();
|
}
|
return result;
|
}
|
|
/**
|
* 组装API地址字符串
|
* @param url API地址
|
* @param params 请求参数
|
* @return 组装好的API地址字符串
|
*/
|
public static String buildAPIUrl(String url, Map<String, Object> params)
|
{
|
if(params.isEmpty()) {
|
return url;
|
}
|
StringBuffer buffer = new StringBuffer(url).append("?");
|
for(String key : params.keySet()) {
|
buffer.append(key).append("=").append(params.get(key)).append("&");
|
}
|
String apiurl = buffer.toString();
|
if(apiurl.endsWith("&")) {
|
return apiurl.substring(0, apiurl.length()-1);
|
} else {
|
return apiurl;
|
}
|
}
|
|
|
}
|