Java短信接口开发经验及具体开发实现

来源:互联网 发布:kindle编辑软件 编辑:程序博客网 时间:2024/05/16 16:26
Java、android使用的短信SDK是多线程:

http://sdk.entinfo.cn:8061/webservice.asmx

其他编程语言使用的是下面接口,希望注意。

http://sdk.entinfo.cn:8060/webservice.asmx

一、Client.class类定义

 

package com;import java.io.*;import java.net.*;import java.security.*;import java.util.regex.Matcher;import java.util.regex.Pattern;public class Client {/* * webservice服务器定义 */private String serviceURL = "http://sdk.entinfo.cn:8061/webservice.asmx";//这里面使用多线程接口,而不是http://sdk.entinfo.cn:8060/webservice.asmxprivate String sn = "";// 序列号private String password = "";private String pwd = "";// 密码/* * 构造函数 */public Client(String sn, String password)throws UnsupportedEncodingException {this.sn = sn;this.password = password;//密码为md5(sn+password)this.pwd = this.getMD5(sn + password);}/* * 方法名称:getMD5  * 功    能:字符串MD5加密  * 参    数:待转换字符串  * 返 回 值:加密之后字符串 */public String getMD5(String sourceStr) throws UnsupportedEncodingException {String resultStr = "";try {byte[] temp = sourceStr.getBytes();MessageDigest md5 = MessageDigest.getInstance("MD5");md5.update(temp);// resultStr = new String(md5.digest());byte[] b = md5.digest();for (int i = 0; i < b.length; i++) {char[] digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8','9', 'A', 'B', 'C', 'D', 'E', 'F' };char[] ob = new char[2];ob[0] = digit[(b[i] >>> 4) & 0X0F];ob[1] = digit[b[i] & 0X0F];resultStr += new String(ob);}return resultStr;} catch (NoSuchAlgorithmException e) {e.printStackTrace();return null;}}/* * 方法名称:mdgetSninfo  * 功    能:获取信息 * 参    数:sn,pwd(软件序列号,加密密码md5(sn+password)) *  */public String mdgetSninfo() {String result = "";String soapAction = "http://entinfo.cn/mdgetSninfo";String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";xml += "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">";xml += "<soap:Body>";xml += "<mdgetSninfo xmlns=\"http://entinfo.cn/\">";xml += "<sn>" + sn + "</sn>";xml += "<pwd>" + pwd + "</pwd>";xml += "<mobile>" + "" + "</mobile>";xml += "<content>" + "" + "</content>";xml += "<ext>" + "" + "</ext>";xml += "<stime>" + "" + "</stime>";xml += "<rrid>" + "" + "</rrid>";xml += "<msgfmt>" + "" + "</msgfmt>";xml += "</mdgetSninfo>";xml += "</soap:Body>";xml += "</soap:Envelope>";URL url;try {url = new URL(serviceURL);URLConnection connection = url.openConnection();HttpURLConnection httpconn = (HttpURLConnection) connection;ByteArrayOutputStream bout = new ByteArrayOutputStream();bout.write(xml.getBytes());byte[] b = bout.toByteArray();httpconn.setRequestProperty("Content-Length", String.valueOf(b.length));httpconn.setRequestProperty("Content-Type","text/xml; charset=gb2312");httpconn.setRequestProperty("SOAPAction", soapAction);httpconn.setRequestMethod("POST");httpconn.setDoInput(true);httpconn.setDoOutput(true);OutputStream out = httpconn.getOutputStream();out.write(b);out.close();InputStreamReader isr = new InputStreamReader(httpconn.getInputStream());BufferedReader in = new BufferedReader(isr);String inputLine;while (null != (inputLine = in.readLine())) {Pattern pattern = Pattern.compile("<mdgetSninfoResult>(.*)</mdgetSninfoResult>");Matcher matcher = pattern.matcher(inputLine);while (matcher.find()) {result = matcher.group(1);}}return result;} catch (Exception e) {e.printStackTrace();return "";}}/* * 方法名称:mdgxsend  * 功    能:发送个性短信  * 参    数:mobile,content,ext,stime,rrid,msgfmt(手机号,内容,扩展码,定时时间,唯一标识,内容编码) * 返 回 值:唯一标识,如果不填写rrid将返回系统生成的 */public String mdgxsend(String mobile, String content, String ext, String stime,String rrid, String msgfmt) {String result = "";String soapAction = "http://entinfo.cn/mdgxsend";String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";xml += "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">";xml += "<soap:Body>";xml += "<mdgxsend xmlns=\"http://entinfo.cn/\">";xml += "<sn>" + sn + "</sn>";xml += "<pwd>" + pwd + "</pwd>";xml += "<mobile>" + mobile + "</mobile>";xml += "<content>" + content + "</content>";xml += "<ext>" + ext + "</ext>";xml += "<stime>" + stime + "</stime>";xml += "<rrid>" + rrid + "</rrid>";xml += "<msgfmt>" + msgfmt + "</msgfmt>";xml += "</mdgxsend>";xml += "</soap:Body>";xml += "</soap:Envelope>";URL url;try {url = new URL(serviceURL);URLConnection connection = url.openConnection();HttpURLConnection httpconn = (HttpURLConnection) connection;ByteArrayOutputStream bout = new ByteArrayOutputStream();bout.write(xml.getBytes());byte[] b = bout.toByteArray();httpconn.setRequestProperty("Content-Length", String.valueOf(b.length));httpconn.setRequestProperty("Content-Type","text/xml; charset=gb2312");httpconn.setRequestProperty("SOAPAction", soapAction);httpconn.setRequestMethod("POST");httpconn.setDoInput(true);httpconn.setDoOutput(true);OutputStream out = httpconn.getOutputStream();out.write(b);out.close();InputStreamReader isr = new InputStreamReader(httpconn.getInputStream());BufferedReader in = new BufferedReader(isr);String inputLine;while (null != (inputLine = in.readLine())) {Pattern pattern = Pattern.compile("<mdgxsendResult>(.*)</mdgxsendResult>");Matcher matcher = pattern.matcher(inputLine);while (matcher.find()) {result = matcher.group(1);}}return result;} catch (Exception e) {e.printStackTrace();return "";}}/* * 方法名称:mdsmssend * 功    能:发送短信  * 参    数:mobile,content,ext,stime,rrid,msgfmt(手机号,内容,扩展码,定时时间,唯一标识,内容编码) * 返 回 值:唯一标识,如果不填写rrid将返回系统生成的 */public String mdsmssend(String mobile, String content, String ext, String stime,String rrid,String msgfmt) {String result = "";String soapAction = "http://entinfo.cn/mdsmssend";String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";xml += "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">";xml += "<soap:Body>";xml += "<mdsmssend  xmlns=\"http://entinfo.cn/\">";xml += "<sn>" + sn + "</sn>";xml += "<pwd>" + pwd + "</pwd>";xml += "<mobile>" + mobile + "</mobile>";xml += "<content>" + content + "</content>";xml += "<ext>" + ext + "</ext>";xml += "<stime>" + stime + "</stime>";xml += "<rrid>" + rrid + "</rrid>";xml += "<msgfmt>" + msgfmt + "</msgfmt>";xml += "</mdsmssend>";xml += "</soap:Body>";xml += "</soap:Envelope>";URL url;try {url = new URL(serviceURL);URLConnection connection = url.openConnection();HttpURLConnection httpconn = (HttpURLConnection) connection;ByteArrayOutputStream bout = new ByteArrayOutputStream();bout.write(xml.getBytes());byte[] b = bout.toByteArray();httpconn.setRequestProperty("Content-Length", String.valueOf(b.length));httpconn.setRequestProperty("Content-Type","text/xml; charset=gb2312");httpconn.setRequestProperty("SOAPAction", soapAction);httpconn.setRequestMethod("POST");httpconn.setDoInput(true);httpconn.setDoOutput(true);OutputStream out = httpconn.getOutputStream();out.write(b);out.close();InputStreamReader isr = new InputStreamReader(httpconn.getInputStream());BufferedReader in = new BufferedReader(isr);String inputLine;while (null != (inputLine = in.readLine())) {Pattern pattern = Pattern.compile("<mdsmssendResult>(.*)</mdsmssendResult>");Matcher matcher = pattern.matcher(inputLine);while (matcher.find()) {result = matcher.group(1);}}return result;} catch (Exception e) {e.printStackTrace();return "";}}}


 

一、普通群发短信mdsmssend

 

参数名称

说明

是否必须有值

备注

sn

软件序列号

格式XXX-XXX-XXX-XXXXX

pwd

密码

md5(sn+password) 32位大写密文

mobile

手机号

必填(支持10000个手机号,建议<=5000)多个英文逗号隔开

Content

内容

支持长短信(详细请看长短信扣费说明),URLUTF8编码

Ext

扩展码

例如:123(默认置空)

stime

定时时间

例如:2010-12-29 16:27:03(置空表示立即发送)

Rrid

唯一标识

最长18位,支持数字。

msgfmt
内容编码
0:ASCII串。3:短信写卡操作。4:二进制信息。空或15:含GB汉字

函数返回值:String(唯一标识,即当前发送短信批次的唯一标识,和rrid对应,如为空则返回系统生成的rrid)

sn即您注册时的序列号,pwd需要MD5(SN+pwd)加密,取32位大写。

接口地址:http://sdk.entinfo.cn:8061/webservice.asmx?op=mdsmssend

示例1

SN= SDK-SSD-010-00001

PWD=3B5D3C427365F40C1D27682D78BB31E0

Mobile:139***404,138***213…………….

Content:测试

Ext: ""

Stime: ""

Rrid: ""

输出结果:

XML格式:

具体Java调用实现:

package com;import java.io.UnsupportedEncodingException;import java.net.*;public class Demo_Client {public static void main(String[] args) throws UnsupportedEncodingException{String sn="SDK-BBX-XXX-XXXXX";String pwd="******";Client client=new Client(sn,pwd);//短信发送    String content=URLEncoder.encode("张三您好,您本月实发工资3999元。[雷雨科技]", "utf8");String result_mt = client.mdsmssend("18636924700", content, "", "", "", "");System.out.print(result_mt);}}


 

二、个性群发短信mdgxsend

 

参数名称

说明

是否必须 有值  

备注

Sn

软件序列号

格式XXX-XXX-XXX-XXXXX

pwd

密码

md5(sn+password)32位大写密文

mobile

手机号

必填(建议一次最多提交1000个)

Content

内容

内容通过小逗号与手机号一一对应好URLUTF8编码

Ext

扩展码

例如:123(默认置空)

Stime

定时时间

例如: 2010-12-30 9:23:20(置空表示立即发送)

Rrid

唯一标识

最长18位

msgfmt

内容编码

0:ASCII串。3:短信写卡操作。4:二进制信息。空或15:含GB汉字

函数返回值:String (唯一标识,即当前发送短信批次的唯一标识,和rrid对应,如为空则返回系统生成的rrid,此处的rrid和mdsmssend发送的rrid用法相同,此方法用于发送多条内容不相同手机号不同多个的情况)

接口地址:http://sdk.entinfo.cn:8061/webservice.asmx?op=mdgxsend

示例1

SN= SDK-SSD-010-00001

PWD=3B5D3C427365F40C1D27682D78BB31E0

Mobile:139***404,138***213…………….

Content:测试,测试…………….

Ext: ""

Stime: ""

Rrid: ""

输出结果:

XML格式:

具体Java调用实现:

package com;import java.io.UnsupportedEncodingException;import java.net.*;public class Demo_Client {public static void main(String[] args) throws UnsupportedEncodingException{String sn="SDK-BBX-XXX-XXXXX";String pwd="******";Client client=new Client(sn,pwd);//个性短信发送String result_gxmt = client.mdgxsend("18636924700,18636800125", "张三您好,您本月实发工资3999元。[雷雨科技],李四您好,您本月实发工资4999元。[雷雨科技]", "", "", "", "");System.out.print(result_gxmt);}}


 

二、webservice返回集合对照表:

 

返回值

返回值说明

问题描述

-2 

帐号/密码不正确

1.序列号未注册2.密码加密不正确3.密码已被修改4.序列号已注销

-4

余额不足支持本次发送(或者修改密码长度不正确)

余额不足(或者修改密码长度不在6位到10位之间)

-5

数据格式错误

只能自行调试了。或与技术支持联系

-6

参数有误

看参数传的是否均正常,请调试程序查看各参数

-7

权限受限

该序列号是否已经开通了调用该方法的权限

-8

流量控制错误

-9

扩展码权限错误

该序列号是否已经开通了扩展子号的权限,把ext这个参数置空。

-10

内容长度长

单字节不能超过1000个字符,双字节不能超过500个字符

-11

内部数据库错误

-12

序列号状态错误

序列号是否被禁用

-14

服务器写文件失败

-17

没有权限

如发送彩信仅限于SDK3

-19

禁止同时使用多个接口地址

每个序列号提交只能使用一个接口地址

-20

相同手机号,相同内容重复提交

-22

Ip鉴权失败

提交的IP不是所绑定的IP

-23

缓存无此序列号信息

-601

序列号为空,参数错误

-602

序列号格式错误,参数错误

-603

密码为空,参数错误

-604

手机号码为空,参数错误

-605

内容为空,参数错误

-606

ext长度大于9,参数错误

-607

参数错误 扩展码非数字 

-608

参数错误 定时时间非日期格式

-609

rrid长度大于18,参数错误 

-610

参数错误 rrid非数字

-611

参数错误 内容编码不符合规范

-623

手机个数与内容个数不匹配

-624

扩展个数与手机个数数

-644 

rrid个数与手机个数不一致

注:以上返回值针对个别方法.请具体参看每个用到方法的详细说明。

三、附加说明:

1.接口地址:

常用接口地址:http://sdk.entinfo.cn:8060/webservice.asmx (一般调用)

多线程接口地址:http://sdk.entinfo.cn:8061/webservice.asmx (java、andriod使用)

2.其它说明:

(1)开发使用的帐号必须为SDK开头,如SDK-SSD-010-00001,帐号第一次需要调用Register方法注册一次.仅需注册一次即可,信息必须真实

(2)UnRegister与Register配合使用, 连续使用不得超过10次/天;

(3)群发推荐使用接口方法 mt或者mdSmsSend (仅方法名不同);

3. 郑重声明:

(1)禁止相同的内容多个手机号连续一条一条提交. 否则禁用帐号,由此带来损失由客户自行负责.

(2)请客户提供外网服务器IP以便于绑定IP发送,提高账号的安全性!

(3)在程序里最好有配置文件,程序自动判断当某个接口连接超时提交速度变慢时.程序可以自动切换其它的接口以下是推荐的几个服务器,仅接口地址不同而已.方法全部相同;

地址1:http://sdk.entinfo.cn:8060/webservice.asmx

地址2:http://sdk2.entinfo.cn:8060/webservice.asmx

这些地址都是标准的webservice地址,C#,Java客户可以按照自己熟悉的方式去解析String   

或者

地址1:http://sdk.entinfo.cn:8060/webservice.asmx?wsdl

地址2:http://sdk2.entinfo.cn:8060/webservice.asmx?wsdl

四、示例Demo源代码下载:

DEMO SDK通用版接口文档  所有下载

 

0 0
原创粉丝点击