短信发送例子

时至今日,短信验证提醒已经司空见惯。一个小例子,记录一下公司集成短信的过程,其实这些demo一般短信服务商都会提供。作为一个常见应用例子,记之。

package com.jq.test;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;
import java.net.URLEncoder;


public class SmsClient {
protected String smsSvcUrl = "http://****:8860"; //服务器URL 地址
protected String cust_code = "****"; //账号
protected String password = "****"; //密码
protected String sp_code = "****"; //接入码(扩展码)

public void sendSms(String mobiles, String content) throws IOException {
sendSms(mobiles, content, sp_code, 0);
}

public void sendSms(String mobiles, String content, long task_id)
throws IOException {
sendSms(mobiles, content, sp_code, task_id);
}

public void sendSms(String mobiles, String content, String sp_code)
throws IOException {
sendSms(mobiles, content, sp_code, 0);
}

public void sendSms(String mobiles, String content, String sp_code,
long task_id) throws IOException {
String urlencContent = URLEncoder.encode(content,"utf-8");
//String sign = MD5.getMD5((urlencContent + password).getBytes());
String sign=MD5.sign(urlencContent, password, "utf-8");
String postData = "content=" + urlencContent + "&destMobiles="
+ mobiles + "&sign=" + sign + "&cust_code=" + cust_code
+ "&sp_code=" + sp_code + "&task_id=" + task_id;
System.err.println(postData);
URL myurl = new URL(smsSvcUrl);
URLConnection urlc = myurl.openConnection();
urlc.setReadTimeout(1000 * 30);
urlc.setDoOutput(true);
urlc.setDoInput(true);
urlc.setAllowUserInteraction(false);

DataOutputStream server = new DataOutputStream(urlc.getOutputStream());
//System.out.println("发送数据=" + postData);

server.write(postData.getBytes("utf-8"));
server.close();

BufferedReader in = new BufferedReader(new InputStreamReader(
urlc.getInputStream(), "utf-8"));
String resXml = "", s = "";
while ((s = in.readLine()) != null)
resXml = resXml + s + "\r\n";
in.close();
System.out.println("接收数据=" + URLDecoder.decode(resXml,"utf-8"));
}

public static void main(String[] args) {
SmsClient client = new SmsClient();
try {
client.sendSms("电话号码", "你好这是测试数据");
} catch (IOException e) {
e.printStackTrace();
}
}
}


package com.jq.test;

import java.io.UnsupportedEncodingException;
import java.security.SignatureException;
import org.apache.commons.codec.digest.DigestUtils;

/**
* 功能:签名处理核心文件
* 修改日期2012-08-17
* */

public class MD5 {

/**
* 签名字符??
* @param text 签名的字符串
* @param key 密钥
* @param input_charset 编码格式
* @return 签名结果
*/
public static String sign(String text, String key, String input_charset) {
text = text + key;
return DigestUtils.md5Hex(getContentBytes(text, input_charset));
}

/**
* 签名字符?
* @param text 签名的字符串
* @param sign 签名结果
* @param key 密钥
* @param input_charset 编码格式
* @return 签名结果
*/
public static boolean verify(String text, String sign, String key, String input_charset) {
text = text + key;
String mysign = DigestUtils.md5Hex(getContentBytes(text, input_charset));
if(mysign.equals(sign)) {
return true;
}
else {
return false;
}
}

/**
* @param content
* @param charset
* @return
* @throws SignatureException
* @throws UnsupportedEncodingException
*/
private static byte[] getContentBytes(String content, String charset) {
if (charset == null || "".equals(charset)) {
return content.getBytes();
}
try {
return content.getBytes(charset);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("MD5签名过程中出现错??指定的编码集不对,您目前指定的编码集是:" + charset);
}
}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值