集成华软通信短信网关,实现发短信功能,具体代码如下:
package com.zrsc.sendsms;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;public class SMS106 {
private static String NOTESTARTSIGN="【***】";//短信前的签名
private static String USERNAME="***";//发短信的账号
private static String PASSWORD="******************";//发短信密码对应的MD5值
private static String GWID="***";//短信网关ID
private static String VERIFICATION_CODE=NOTESTARTSIGN+"您的验证码是:{code}";//短信验证码例子
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(sendNode(VERIFICATION_CODE.replace("{code}", "132312"),"183***"));
}
/**
* http://sms.106txt.com/main.htm
* @param data
* @return {"returnstatus":"success","code":"0","taskID":"201811131817587161"}
*/
public static String sendNode(String data,String mobile) {
String result;
try {
if(!data.startsWith(NOTESTARTSIGN)){
data=NOTESTARTSIGN+data;
}
result = postNote("http://jk.106txt.com/smsUTF8.aspx?type=send&username="+USERNAME+"&password="+PASSWORD+"&gwid="+GWID+"&mobile="+mobile+"&message="+data+"&rece=json",
"".toString().getBytes("GBK"), "GBK");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
result = "";
}return result;
}
private static String postNote(final String url, final byte[] data,
String contentEncoding) {
String result = null;
int bufferSize = 1024;
HttpURLConnection httpURLConnection = null;
InputStream inputStream = null;
OutputStream outputStream = null;
ByteArrayOutputStream byteArrayOutputStream = null;
// String contentEncoding;
byte[] buffer = new byte[bufferSize];
int readBytes = 0;
try {
httpURLConnection = (HttpURLConnection) (new URL(url)
.openConnection());
httpURLConnection.setConnectTimeout(30000);
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setUseCaches(false);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
httpURLConnection.setRequestProperty("Charset", "gb2312");// add post data
outputStream = new DataOutputStream(
httpURLConnection.getOutputStream());
outputStream.write(data);
outputStream.flush();
// get response
// getCookie(httpURLConnection);
byteArrayOutputStream = new ByteArrayOutputStream();
if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
inputStream = new BufferedInputStream(
httpURLConnection.getInputStream());
while ((readBytes = inputStream.read(buffer)) > 0) {
byteArrayOutputStream.write(buffer, 0, readBytes);
}
}
result = new String(byteArrayOutputStream.toByteArray(),
contentEncoding);
} catch (Exception ex) {
ex.printStackTrace();
return null;
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}if (byteArrayOutputStream != null) {
try {
byteArrayOutputStream.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
if (httpURLConnection != null) {
try {
httpURLConnection.disconnect();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
return result;
}
}
发送短信成功后,会返回如下信息:
{"returnstatus":"success","code":"0","taskID":"201811131817587161"}
源码包含如下
1.集成华软通信短信网关,已实现发验证码短信,对应Java文件为SMS106.java,没有依赖的jar。
2.集成河南华夏通信短信网关,已实现发验证码短信,对应Java文件为ShenZhouMsg.javar,没有依赖的jar。
3.集成云通信的企业信使短信平台,已实现发验证码短信,对应Java文件为MessageSenderUtil.javar,依赖的jar有httpclient-4.3.3.jar,httpcore-4.3.2.jar,commons-logging-1.1.1.jar,dom4j-1.6.1.jar。
4.集成阿里云短信服务,已实现发验证码短信,对应Java文件为AliSMS.java,依赖的jar有aliyun-java-sdk-core-4.1.0.jar,activation-1.1.1.jar,gson-2.8.5.jar,jaxb-api-2.1.jar,jaxb-core-2.3.0.1.jar,jaxb-impl-2.3.2.jar,httpclient-4.3.3.jar,httpcore-4.3.2.jar。
阿里云短信服务有对接文档,即《阿里短信集成详解.docx》。
综上所述,费用最便宜是集成阿里云短信服务,但技术对接最麻烦。如果想找个最简单对接又便宜的,建议使用河南华夏通信短信网关。