实现第三方短信接口的工具类怎么写
发布网友
发布时间:2022-04-22 12:50
我来回答
共1个回答
热心网友
时间:2023-06-28 16:25
package com.szqbl.qxfz.io;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.URIException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.util.URIUtil;
import org.apache.commons.lang3.StringUtils;
import com.szqbl.lib.util.MD5Util;
public class SMSUtil {
public static void sendSMS_GM(List<String> mobileno, String content) {
String eprId = "***";
String userId = "******";//用户名
String pwd = "******";//密码
long timestamp = System.currentTimeMillis();
int msgId = (int) (Math.random() * 1000);
String key = MD5Util.string2MD5(eprId + userId + pwd + timestamp);
String mobile = "";
for (String tel : mobileno) {
mobile += isMobile(tel) ? tel + "," : "";
}
if (StringUtils.isNotEmpty(mobile)) {
mobile = mobile.substring(0, mobile.length() - 1);
}
String url = "http://client.sms10000.com/***";
String parmString = "?cmd=send&eprId=" + eprId + "&userId=" + userId + "&key=" + key + "×tamp=" + timestamp
+ "&format=1&mobile=" + mobile + "&msgId=" + msgId + "&content=" + content;
// 发送短信
String result = doGet(url, parmString, "utf-8", false);
System.out.println(result);
}
/**
* 手机号验证
*
* @param str
* @return 验证通过返回true
*/
public static boolean isMobile(String str) {
Pattern p = null;
Matcher m = null;
boolean b = false;
p = Pattern.compile("^[1][3,4,5,8][0-9]{9}$"); // 验证手机号
m = p.matcher(str);
b = m.matches();
return b;
}
/**
* 执行一个HTTP GET请求,返回请求响应的HTML
*
* @param url
* 请求的URL地址
* @param queryString
* 请求的查询参数,可以为null
* @param charset
* 字符集
* @param pretty
* 是否美化
* @return 返回请求响应的HTML
*/
public static String doGet(String url, String queryString, String charset, boolean pretty) {
StringBuffer response = new StringBuffer();
HttpClient client = new HttpClient();
HttpMethod method = new GetMethod(url);
try {
if (queryString != null && !queryString.equals(""))
// 对get请求参数做了http请求默认编码,好像没有任何问题,汉字编码后,就成为%式样的字符串
method.setQueryString(URIUtil.encodeQuery(queryString));
client.executeMethod(method);
if (method.getStatusCode() == HttpStatus.SC_OK) {
BufferedReader reader = new BufferedReader(
new InputStreamReader(method.getResponseBodyAsStream(), charset));
String line;
while ((line = reader.readLine()) != null) {
if (pretty)
response.append(line).append(System.getProperty("line.separator"));
else
response.append(line);
}
reader.close();
}
} catch (URIException e) {
} catch (IOException e) {
} finally {
method.releaseConnection();
}
return response.toString();
}
public static void main(String[] args) throws Exception {
List<String> tels = new ArrayList<String>();
tels.add("18288888888");
sendSMS_GM(tels, "短信main测试!");
}
}
这个得根据你的短信接口文档来,大概也就这样,写的比较简单,忘采纳!