虚拟主机域名注册-常见问题短信通道问题 → 程序实例


短信通道:JAVA实例

请求地址
名称是否必须示例值说明
请求地址   UTF-8http://sms.kingtto.com:9999/sms.aspx如果服务器不支持解析,将sms.kingtto.com改为101.201.41.194
请求参数
名称是否必须示例值描述
action必须固定设置为:send任务命令为:短信发送
account        必须ceshi注册获取;平台登陆账号。  点此注册>>
password    必须123456    客服提供;短信平台登陆密码。  平台注册后联系客服为您生成
userid    必须1001   客服提供;短信平台个人ID。  平台注册后联系客服为您生成
mobile必须13000000000,13000000001短信接收号码。支持单个或多个手机号码,传入号码为11位手机号码,不能加0或+86。群发短信需传入多个号码,以英文逗号分隔,一次调用最多传入200个号码。示例:13000000000,13000000001
content必须【Pachira】您的验证码是:123456发送短信的内容,整体做urlencode.短信签名的格式为:【签名】放在内容的最前方。 
示范:【Pachira】您本次注册的验证码是:888888,
其中“签名”可根据自己公司的名称或简称自定义,字数为2-8字,不可以是纯数字、电话号码或者网址,不可以是无意义的签名,比如“大家好”“哈哈哈”“提示”等
正确示例:【Pachira】、【京东】、【百度】、【糯米团购】
错误示例:【你好】、【通知】、【测试一下】、【哈哈哈】、【123123】
sendtime可选2000-12-31 00:00:10短信定时发送时间。不设置默认为立即发送。格式为:YYYY-MM-DD HH:MM:SS
JAVA实例

public class SmsClientSend {
/*
* @param url
* :必填--发送连接地址URL——http://sms.kingtto.com:9999/sms.aspx
* @param userid
* :必填--用户ID,为数字
* @param account
* :必填--用户帐号
* @param password
* :必填--用户密码
* @param mobile
* :必填--发送的手机号码,多个可以用逗号隔比如>130xxxxxxxx,131xxxxxxxx
* @param content
* :必填--实际发送内容,
* @param action
* :选填--访问的事件,默认为send
* @param sendType
* :选填--发送方式,默认为POST
* @param codingType
* :选填--发送内容编码方式,默认为UTF-8
* @param backEncodType
* :选填--返回内容编码方式,默认为UTF-8
* @return 返回发送之后收到的信息
*/
private static String sendSms(String url, String userid, String account,
String password, String mobile, String content, String action,
String sendType, String codingType, String backEncodType) {

try {
if (codingType == null || codingType.equals("")) {
codingType = "UTF-8";
}
if (backEncodType == null || backEncodType.equals("")) {
backEncodType = "UTF-8";
}
StringBuffer send = new StringBuffer();
if (action != null && !action.equals("")) {
send.append("action=").append(action);
} else {
send.append("action=send");
}

send.append("&userid=").append(userid);
send.append("&account=").append(
URLEncoder.encode(account, codingType));
send.append("&password=").append(
URLEncoder.encode(password, codingType));
send.append("&mobile=").append(mobile);
send.append("&content=").append(
URLEncoder.encode(content, codingType));
if (sendType != null && (sendType.toLowerCase()).equals("get")) {
return SmsClientAccessTool.getInstance().doAccessHTTPGet(
url + "?" + send.toString(), backEncodType);
} else {
return SmsClientAccessTool.getInstance().doAccessHTTPPost(url,
send.toString(), backEncodType);
}
} catch (Exception e) {
e.printStackTrace();
return "未发送,编码异常";
}
}

}
public class SmsClientAccessTool {

private static SmsClientAccessTool smsClientToolInstance;

/**
* 采用单列方式来访问操作
*
* @return
*/
public static synchronized SmsClientAccessTool getInstance() {

if (smsClientToolInstance == null) {
smsClientToolInstance = new SmsClientAccessTool();
}
return smsClientToolInstance;
}

/**
*

* POST方法
*

*
* @param sendUrl
* :访问URL
* @param paramStr
* :参数串
* @param backEncodType
* :返回的编码
* @return
*/
public String doAccessHTTPPost(String sendUrl, String sendParam,
String backEncodType) {

StringBuffer receive = new StringBuffer();
BufferedWriter wr = null;
try {
if (backEncodType == null || backEncodType.equals("")) {
backEncodType = "UTF-8";
}

URL url = new URL(sendUrl);
HttpURLConnection URLConn = (HttpURLConnection) url
.openConnection();

URLConn.setDoOutput(true);
URLConn.setDoInput(true);
((HttpURLConnection) URLConn).setRequestMethod("POST");
URLConn.setUseCaches(false);
URLConn.setAllowUserInteraction(true);
HttpURLConnection.setFollowRedirects(true);
URLConn.setInstanceFollowRedirects(true);

URLConn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded;charset=UTF-8");
URLConn.setRequestProperty("Content-Length", String
.valueOf(sendParam.getBytes().length));

DataOutputStream dos = new DataOutputStream(URLConn
.getOutputStream());
dos.writeBytes(sendParam);

BufferedReader rd = new BufferedReader(new InputStreamReader(
URLConn.getInputStream(), backEncodType));
String line;
while ((line = rd.readLine()) != null) {
receive.append(line).append("\r\n");
}
rd.close();
} catch (java.io.IOException e) {
receive.append("访问产生了异常-->").append(e.getMessage());
e.printStackTrace();
} finally {
if (wr != null) {
try {
wr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
wr = null;
}
}

return receive.toString();
}

public String doAccessHTTPGet(String sendUrl, String backEncodType) {

StringBuffer receive = new StringBuffer();
BufferedReader in = null;
try {
if (backEncodType == null || backEncodType.equals("")) {
backEncodType = "UTF-8";
}

URL url = new URL(sendUrl);
HttpURLConnection URLConn = (HttpURLConnection) url
.openConnection();

URLConn.setDoInput(true);
URLConn.setDoOutput(true);
URLConn.connect();
URLConn.getOutputStream().flush();
in = new BufferedReader(new InputStreamReader(URLConn
.getInputStream(), backEncodType));

String line;
while ((line = in.readLine()) != null) {
receive.append(line).append("\r\n");
}

} catch (IOException e) {
receive.append("访问产生了异常-->").append(e.getMessage());
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (java.io.IOException ex) {
ex.printStackTrace();
}
in = null;

}
}

return receive.toString();
}
}





来源:
阅读:423
日期:2018-09-07

【 双击滚屏 】 【 推荐朋友 】 【 收藏 】 【 打印 】 【 关闭 】 【 字体: 】 
上一篇:短信通道:C#.net实例
下一篇:短信通道:OBJECTC实例
  >> 相关文章