近来由于项目需要,需要用到手机短信验证码的功能,其中最主要的是用到了第三方提供的短信平台接口WebService客户端接口,下面我把我在项目中用到的记录一下,以便给大家提供个思路,由于本人的文采有限,还请大家见谅!
一:首先上几张案例截图,以便大家可以了解一下短信验证码的流程,这里我在做的过程中也参考了很多网站的短信验证码,比如:58同城,汽车之家等。
1.点击获取验证码之前的样式:
2.输入正确的手机号后点击获取验证码之后的样式:
3.如果手机号已经被注册的样式:
4.如果一个手机号一天发送超过3次就提示不能发送:
二:前台的注册页面的代码:reg.jsp
三:前台获取短信验证码的js:sms.js
[javascript] view plain
copy
- var InterValObj;
- var count = 120;
- var curCount;
- var code = "";
- var codeLength = 6;
-
- function sendMessage() {
- curCount = count;
- var jbPhone = $("#jbPhone").val();
- var jbPhoneTip = $("#jbPhoneTip").text();
- if (jbPhone != "") {
- if(jbPhoneTip == "√ 该手机号码可以注册,输入正确" || jbPhoneTip == "√ 短信验证码已发到您的手机,请查收"){
-
- for ( var i = 0; i < codeLength; i++) {
- code += parseInt(Math.random() * 9).toString();
- }
-
- $("#btnSendCode").attr("disabled", "true");
- $("#btnSendCode").val("请在" + curCount + "秒内输入验证码");
- InterValObj = window.setInterval(SetRemainTime, 1000);
-
- $.ajax({
- type: "POST",
- dataType: "text",
- url: "UserAction_sms.action",
- data: "jbPhone=" + jbPhone +"&code=" + code,
- error: function (XMLHttpRequest, textStatus, errorThrown) {
-
- },
- success: function (data){
- data = parseInt(data, 10);
- if(data == 1){
- $("#jbPhoneTip").html("√ 短信验证码已发到您的手机,请查收");
- }else if(data == 0){
- $("#jbPhoneTip").html("× 短信验证码发送失败,请重新发送");
- }else if(data == 2){
- $("#jbPhoneTip").html("× 该手机号码今天发送验证码过多");
- }
- }
- });
- }
- }else{
- $("#jbPhoneTip").html("× 手机号码不能为空");
- }
- }
-
-
- function SetRemainTime() {
- if (curCount == 0) {
- window.clearInterval(InterValObj);
- $("#btnSendCode").removeAttr("disabled");
- $("#btnSendCode").val("重新发送验证码");
- code = "";
- }else {
- curCount--;
- $("#btnSendCode").val("请在" + curCount + "秒内输入验证码");
- }
- }
-
- $(document).ready(function() {
- $("#SmsCheckCode").blur(function() {
- var SmsCheckCodeVal = $("#SmsCheckCode").val();
-
- $.ajax({
- url : "UserAction_checkCode.action",
- data : {SmsCheckCode : SmsCheckCodeVal},
- type : "POST",
- dataType : "text",
- success : function(data) {
- data = parseInt(data, 10);
- if (data == 1) {
- $("#SmsCheckCodeTip").html("√ 短信验证码正确,请继续");
- } else {
- $("#SmsCheckCodeTip").html("× 短信验证码有误,请核实后重新填写");
- }
- }
- });
- });
- });
四:验证码用户名和手机号码的js:register.js
[javascript] view plain
copy
-
- function trim(str) {
- var strnew = str.replace(/^\s*|\s*$/g, "");
- return strnew;
- }
-
- function checkusername() {
- var username = document.form1.username.value;
- if (username == "" || !isNaN(username.charAt(0))) {
- document.getElementById("usernameTip").innerHTML = "× 首字母不能为数字或者用户名不能为空";
- return false;
- } else if (username.length < 6 || username.length > 30) {
- document.getElementById("usernameTip").innerHTML = "× 用户名长度为6-30位字符";
- return false;
- } else {
- document.getElementById("usernameTip").innerHTML = "√ 用户名合格";
-
- $.ajax({
- url : "UserAction_checkUserName.action",
- data : {username : username},
- type : "POST",
- dataType : "text",
- success : function(data) {
- data = parseInt(data, 10);
- if (data != 0) {
- $("#usernameTip").html("× 该用户名已经被注册,请重新输入");
- } else {
- $("#usernameTip").html("√ 该用户名可以注册,输入正确");
- }
- }
- });
- return true;
- }
- }
-
- function checkpassword() {
- var password = document.form1.password.value;
- if (password.length < 6 || password.length > 30) {
- document.getElementById("passwordTip").innerHTML = "× 密码长度不能小于6位,大于30位";
- return false;
- } else if (!isNaN(password)) {
- document.getElementById("passwordTip").innerHTML = "× 密码不能全是数字";
- return false;
- } else {
- document.getElementById("passwordTip").innerHTML = "√ 密码合格";
- return true;
- }
- }
-
- function checkpasswrodb() {
- var password = document.form1.password.value;
- var passwordRepeat = document.form1.passwordRepeat.value;
- if (trim(password) != trim(passwordRepeat)) {
- document.getElementById("passwordRepeatTip").innerHTML = "× 两次密码输入必须一致";
- return false;
- } else {
- document.getElementById("passwordRepeatTip").innerHTML = "√ 密码输入一致";
- return true;
- }
- }
-
- function checknickname() {
- var nickname = document.form1.nickname.value;
- if (trim(nickname) == "") {
- document.getElementById("nicknameTip").innerHTML = "× 姓名不能为空";
- return false;
- } else {
- document.getElementById("nicknameTip").innerHTML = "√ 姓名输入正确";
- return true;
- }
- }
-
- function checkjbPhone() {
- var jbPhone = document.form1.jbPhone.value;
- var re= /(^1[3|5|8][0-9]{9}$)/;
- if (trim(jbPhone) == "") {
- document.getElementById("jbPhoneTip").innerHTML = "× 手机号码不能为空";
- return false;
- } else if(trim(jbPhone) != ""){
- if(!re.test(jbPhone)){
- document.getElementById("jbPhoneTip").innerHTML = "× 请输入有效的手机号码";
- return false;
- }else{
- document.getElementById("jbPhoneTip").innerHTML = "√ 手机号码输入正确";
-
- $.ajax({
- url : "UserAction_checkPhone.action",
- data : {jbPhone : jbPhone},
- type : "POST",
- dataType : "text",
- success : function(data) {
- data = parseInt(data, 10);
- if (data != 0) {
- $("#jbPhoneTip").html("× 该手机号码已经被注册,请重新输入");
- } else {
- $("#jbPhoneTip").html("√ 该手机号码可以注册,输入正确");
- }
- }
- });
- return true;
- }
- }
-
- }
-
- function checkjbCredentialsCode() {
- var jbCredentialsCode = document.form1.jbCredentialsCode.value;
- var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
- if (trim(jbCredentialsCode) == "") {
- document.getElementById("jbCredentialsCodeTip").innerHTML = "× 证件号码不能为空";
- return false;
- } else if(trim(jbCredentialsCode) != ""){
- if(!reg.test(jbCredentialsCode)){
- document.getElementById("jbCredentialsCodeTip").innerHTML = "× 请输入合法的证件号码";
- return false;
- }else{
- document.getElementById("jbCredentialsCodeTip").innerHTML = "√ 证件号码输入正确";
-
- $.ajax({
- url : "UserAction_checkCredentialsCode.action",
- data : {jbCredentialsCode : jbCredentialsCode},
- type : "POST",
- dataType : "text",
- success : function(data) {
- data = parseInt(data, 10);
- if (data != 0) {
- $("#jbCredentialsCodeTip").html("× 该证件号码已经被注册,请重新输入");
- } else {
- $("#jbCredentialsCodeTip").html("√ 该证件号码可以注册,输入正确");
- }
- }
- });
- return true;
- }
- }
- }
-
- function checkjbSourceArea() {
- var jbSourceArea = document.form1.jbSourceArea.value;
- if (trim(jbSourceArea) == "") {
- document.getElementById("jbSourceAreaTip").innerHTML = "× 请选择所在地区";
- return false;
- } else {
- document.getElementById("jbSourceAreaTip").innerHTML = "√ 所在地区选择正确";
- return true;
- }
- }
-
- function checkNumber() {
- var checkNum = document.form1.checkNum.value;
- if (trim(checkNum) == "") {
- document.getElementById("checkNumTip").innerHTML = "× 验证码不能为空";
- return false;
- } else {
- document.getElementById("checkNumTip").innerHTML = "√ 验证码合格";
-
- $.ajax({
- url : "UserAction_checkNum.action",
- data : {checkNum : checkNum},
- type : "POST",
- dataType : "text",
- success : function(data) {
- data = parseInt(data, 10);
- if (data != 0) {
- $("#checkNumTip").html("× 验证码错误");
- } else {
- $("#checkNumTip").html("√ 验证码正确");
- }
- }
- });
- return true;
- }
- }
五:后台action的代码:UserAction.java(这里只是调用了我们的第三方给提供的客户端代码的发送短信的方法,如果是调用其他的短信平台,可以换成他们的发送短信的方法,这里只是给大家做个参考)
- package cn.gov.csrc.base.systemmanager.action;
-
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.List;
-
- import javax.annotation.Resource;
- import javax.servlet.http.HttpServletResponse;
-
- import org.apache.log4j.Logger;
- import org.apache.struts2.convention.annotation.Action;
- import org.apache.struts2.convention.annotation.Result;
- import org.apache.struts2.convention.annotation.Results;
- import org.springframework.context.annotation.Scope;
- import org.springframework.security.authentication.encoding.MessageDigestPasswordEncoder;
- import org.springframework.security.core.context.SecurityContextHolder;
- import org.springframework.stereotype.Controller;
-
- import cn.gov.csrc.base.action.BaseAction;
- import cn.gov.csrc.base.log.service.LogService;
- import cn.gov.csrc.base.report.util.CommonUtil;
- import cn.gov.csrc.base.report.util.Env;
- import cn.gov.csrc.base.systemmanager.model.Count;
- import cn.gov.csrc.base.systemmanager.model.User;
- import cn.gov.csrc.base.systemmanager.service.CountService;
- import cn.gov.csrc.base.systemmanager.service.UserService;
- import cn.gov.csrc.base.util.WebContextUtil;
-
- import com.sms.webservice.client.SmsReturnObj;
- import com.sms.webservice.client.SmsWebClient;
-
-
-
-
-
-
-
-
-
- @Controller()
- @Scope("prototype")
- @Results({
- @Result(name = "success", location = "/statics/report.jsp"),
- @Result(name = "modifyPassword", location = "/statics/modifyPassword.jsp")
- })
- public class UserAction extends BaseAction {
-
- private static final long serialVersionUID = 1214696686677691191L;
- private static Logger log = Logger.getLogger(UserAction.class);
- private static final String url = Env.getInstance().getProperty("url");
- private static final String userName = Env.getInstance().getProperty(
- "username");
- private static final String passWord = Env.getInstance().getProperty(
- "password");
-
- private Integer id;
-
- private String username;
-
- private String password;
-
- private String passwordRepeat;
-
- private String jbPhone;
-
- private String jbCredentialsCode;
-
- private String checkNum;
-
- private String newPassword;
-
- private String confirmPassword;
-
- private Integer[] ownRoleIds;
-
- private UserService userService;
-
- private CountService countService;
-
- private LogService logService;
-
- private Count count = new Count();
-
- public Integer getId() {
- return id;
- }
-
- public void setId(Integer id) {
- this.id = id;
- }
-
- public String getUsername() {
- return username;
- }
-
- public void setUsername(String username) {
- this.username = username;
- }
-
- public String getPassword() {
- return password;
- }
-
- public void setPassword(String password) {
- this.password = password;
- }
-
- public String getPasswordRepeat() {
- return passwordRepeat;
- }
-
- public void setPasswordRepeat(String passwordRepeat) {
- this.passwordRepeat = passwordRepeat;
- }
-
- public String getJbPhone() {
- return jbPhone;
- }
-
- public void setJbPhone(String jbPhone) {
- this.jbPhone = jbPhone;
- }
-
- public String getJbCredentialsCode() {
- return jbCredentialsCode;
- }
-
- public void setJbCredentialsCode(String jbCredentialsCode) {
- this.jbCredentialsCode = jbCredentialsCode;
- }
-
- public String getCheckNum() {
- return checkNum;
- }
-
- public void setCheckNum(String checkNum) {
- this.checkNum = checkNum;
- }
-
- public Integer[] getOwnRoleIds() {
- return ownRoleIds;
- }
-
- public void setOwnRoleIds(Integer[] ownRoleIds) {
- this.ownRoleIds = ownRoleIds;
- }
-
- public String getNewPassword() {
- return newPassword;
- }
-
- public void setNewPassword(String newPassword) {
- this.newPassword = newPassword;
- }
-
- public String getConfirmPassword() {
- return confirmPassword;
- }
-
- public void setConfirmPassword(String confirmPassword) {
- this.confirmPassword = confirmPassword;
- }
-
- @Resource
- public void setUserService(UserService userService) {
- this.userService = userService;
- }
-
- public UserService getUserService() {
- return userService;
- }
-
- @Resource
- public void setCountService(CountService countService) {
- this.countService = countService;
- }
-
- public CountService getCountService() {
- return countService;
- }
-
- public LogService getLogService() {
- return logService;
- }
-
- @Resource
- public void setLogService(LogService logService) {
- this.logService = logService;
- }
-
- public Count getCount() {
- return count;
- }
-
- public void setCount(Count count) {
- this.count = count;
- }
-
- public void prepareToUpdatePage() {
- model = userService.getEntity(id);
- }
-
-
-
-
-
-
- @Action(value = "UserAction_login")
- public String login() {
- Object u = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
- if (u instanceof User && u != null) {
- WebContextUtil.getSession().setAttribute("user", u);
- User user = (User)u;
-
- logService.saveTblBaseLog(WebContextUtil.getRequest(),"当前操作人【"+user.getNickname()+"】登录系统");
- } else {
-
- return "login";
- }
- return SUCCESS;
- }
-
-
-
-
- @Action(value = "UserAction_updateUser")
- public String updateUser() {
- if (!newPassword.equals(confirmPassword)) {
- this.setMessage("新密码与确认密码不一致!");
- return "modifyPassword";
- }
- User u = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
- MessageDigestPasswordEncoder encoder = new MessageDigestPasswordEncoder("md5");
- String psw = encoder.encodePassword(newPassword, null);
- userService.modifyPassword(u, psw);
- this.setMessage("保存成功!");
- return "modifyPassword";
- }
-
-
-
-
-
-
- @Action(value = "UserAction_checkUserName")
- public void checkUserName(){
- String result = "0";
- try {
- List list = userService.findUserByUserName(username);
- if(list != null && list.size() > 0){
- result = "1";
- }else{
- result = "0";
- }
- HttpServletResponse response = WebContextUtil.getResponse();
- response.setContentType("application/json;charset=UTF-8");
- response.setHeader("Cache-Control", "no-cache");
- PrintWriter out = response.getWriter();
- out.write(result.toString());
- } catch (IOException e) {
- throw new RuntimeException("验证身份证号码出错", e);
- }
- }
-
-
-
-
-
-
- @Action(value = "UserAction_checkPassWord")
- public void checkPassWord() throws Exception {
- String result = "0";
- MessageDigestPasswordEncoder encoder = new MessageDigestPasswordEncoder("md5");
- String pwd = encoder.encodePassword(password, null);
- List user = userService.findUserByPassWord(pwd);
- if (user != null && user.size() > 0) {
- result = "1";
- }else{
- result = "0";
- }
- HttpServletResponse response = WebContextUtil.getResponse();
- response.setContentType("application/json;charset=UTF-8");
- response.setHeader("Cache-Control", "no-cache");
- PrintWriter out = response.getWriter();
- out.write(result.toString());
- }
-
-
-
-
- @Action(value = "UserAction_checkPhone")
- public void checkPhone(){
- String result = "0";
- try {
- List list = userService.findUserByPhone(jbPhone);
- if(list != null && list.size() > 0){
- result = "1";
- }else{
- result = "0";
- }
- HttpServletResponse response = WebContextUtil.getResponse();
- response.setContentType("application/json;charset=UTF-8");
- response.setHeader("Cache-Control", "no-cache");
- PrintWriter out = response.getWriter();
- out.write(result.toString());
- } catch (IOException e) {
- throw new RuntimeException("验证手机号码出错", e);
- }
- }
-
-
-
-
- @Action(value = "UserAction_checkCredentialsCode")
- public void checkCredentialsCode(){
- String result = "0";
- try {
- List list = userService.findUserByCredentialsCode(jbCredentialsCode);
- if(list != null && list.size() > 0){
- result = "1";
- }else{
- result = "0";
- }
- HttpServletResponse response = WebContextUtil.getResponse();
- response.setContentType("application/json;charset=UTF-8");
- response.setHeader("Cache-Control", "no-cache");
- PrintWriter out = response.getWriter();
- out.write(result.toString());
- } catch (IOException e) {
- throw new RuntimeException("验证身份证号码出错", e);
- }
- }
-
-
-
-
- @Action(value = "UserAction_checkNum")
- public void checkNum(){
- try {
- String result = "0";
- String check_number_key = (String)WebContextUtil.getSession().getAttribute("CHECK_NUMBER_KEY");
- if(checkNum != check_number_key && !checkNum.equals(check_number_key)){
- result = "1";
- }else{
- result = "0";
- }
- HttpServletResponse response = WebContextUtil.getResponse();
- response.setContentType("application/json;charset=UTF-8");
- response.setHeader("Cache-Control", "no-cache");
- PrintWriter out = response.getWriter();
- out.write(result.toString());
- } catch (IOException e) {
- throw new RuntimeException("验证验证码出错", e);
- }
- }
-
-
-
-
-
-
- @Action(value = "UserAction_checkCode")
- public void checkCode() throws Exception{
- String result = "0";
-
- String SmsCheckCode = WebContextUtil.getRequest().getParameter("SmsCheckCode");
-
- String code = (String) WebContextUtil.getSession().getAttribute("code");
- try {
- if(SmsCheckCode != code && !SmsCheckCode.equals(code)){
- result = "0";
- }else{
- result = "1";
- }
- } catch (Exception e) {
- throw new RuntimeException("短信验证失败", e);
- }
- HttpServletResponse response = WebContextUtil.getResponse();
- response.setContentType("application/json;charset=UTF-8");
- response.setHeader("Cache-Control", "no-cache");
- PrintWriter out = response.getWriter();
- out.write(result.toString());
- }
-
-
-
-
-
-
- @Action(value = "UserAction_sms")
- public void sms() throws Exception {
- String result = "0";
-
- String jbPhone = WebContextUtil.getRequest().getParameter("jbPhone");
-
- String code = WebContextUtil.getRequest().getParameter("code");
-
- WebContextUtil.getSession().setAttribute("code", code);
-
- if(!initClient()) {
- return;
- }
-
-
- String mobilephone = jbPhone;
-
- String content = "根据中国证监会举报中心委托,特向您发送此条短信。您的注册验证码为:" + code;
-
- Integer operId = Integer.parseInt(Env.getInstance().getProperty("operId"));
-
- String tosend_time = "";
-
- int sms_id = 0;
-
- short backlist_filter = 0;
-
- short fbdword_filter = 0;
-
- short priority = 0;
-
- String valid_time = "";
-
- int messageCount = countService.findAllRecord(mobilephone);
- log.info("已发短信条数为:" +messageCount);
- if(messageCount < 5){
-
- if (!sendMessage(mobilephone, content, operId, tosend_time, sms_id,
- backlist_filter, fbdword_filter, priority, valid_time)) {
- result = "0";
- } else {
- result = "1";
-
- count.setPhone(mobilephone);
- count.setCaptcha(code);
- count.setSendTime(CommonUtil.getNowDate());
- if(count != null){
- countService.saveEntity(count);
- log.info("短信验证码发送记录保存成功!");
- }
- }
- }else{
- result = "2";
- log.info("该手机号码今天发送验证码过多");
- }
- HttpServletResponse response = WebContextUtil.getResponse();
- response.setContentType("application/json;charset=UTF-8");
- response.setHeader("Cache-Control", "no-cache");
- PrintWriter out = response.getWriter();
- out.write(result.toString());
- }
-
-
-
-
-
- public static boolean initClient() {
-
-
-
- if (!SmsWebClient.enable()) {
- int ret = 0;
- try {
- ret = SmsWebClient.init(url, userName, passWord);
- if (ret == -1 || !SmsWebClient.enable()) {
- log.info("短信平台接口初始化失败!");
- return false;
- }
- log.info("短信平台接口初始化成功!" + ret + "-----");
- } catch (Exception ex) {
- ex.printStackTrace();
- log.info("短信平台接口初始化过程中异常!");
- }
- }
- return true;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public static boolean sendMessage(java.lang.String mobilephone,
- java.lang.String content, int operId, java.lang.String tosend_time,
- int sms_id, short backlist_filter, short fbdword_filter,
- short priority, java.lang.String valid_time) {
-
- try {
- SmsReturnObj retObj = SmsWebClient.webSendMessage(mobilephone,
- content, operId, tosend_time, sms_id, backlist_filter,
- fbdword_filter, priority, valid_time);
- if (retObj.getReturnCode() != 1) {
- log.info("短信发送失败,原因为:" + retObj.getReturnMsg());
- return false;
- } else {
- log.info("短信发送成功!返回结果为:" + retObj.getReturnMsg());
- return true;
- }
- } catch (Exception ex) {
- ex.printStackTrace();
- log.info("短信发送过程发生异常!");
- }
- return true;
- }
-
-
- }
六:具体的短信接口demo到这里下,这里你下载了也不能成功的发送短信,因为你必须要短信平台商给你提供:url,userName,passWord,operId,以及短信平台商还要绑定你的外网ip地址,这样才能真正的发送短信。
Demo下载链接: http://pan.baidu.com/s/1qWt1Qdm
=============================================================================
WebService实例3:利用WebService给手机发短信
实例说明
通过编程方式实现短信息的发送对很多人来说是一件比较繁杂的事情,目前一般的解决方法是通过计算机和手机的连线,通过对手机编程的语言编写相关的手机短信息程序来实现,而这种方法对于一般人来说是很难达到的,因为此种方法不仅要有很多必备的硬件设备,也要懂得手机编程的相关知识。本实例将通过WebService技术发送手机短信,实例运行效果如图17.10所示。
关键技术
本实例利用一个现有的可发送短信的WebService,这个 WebService是新浪网提供的可供用户直接调用的发送短消息的WebService。这个WebService的地址是http://smsinter.sina.com.cn/ws/smswebservice0101.wsdl。在
这个Service中,提供了一个发送短消息的方法sendXml,此方法的语法如下:
string sendXml (carrier , userid , password , mobilenumber , content , msgtype )
sendXml方法中的6个参数均为string类型,并且sendXml方法的返回值也是string类型。
sendXml方法中的参数说明如表16.3所示。
设计过程
(1)新建一个网站,将其命名为MobileNote,默认主页为Default.aspx。在该页面中主要添加一个GridView 控件和一个Button控件,分别用来显示数据和执行查询操作。
(2)Default.aspx页面中主要控件的类型、名称及用途如表17.3所示。
(3)实例中单击Button1按钮完成短信发送,代码如下:
=============================================================================
这个是方法是使用中国网建提供的SMS短信平台(http://sms.webchinese.cn)进行实现。
所以在做这个的时候需要在中国网建SMS短信平台注册个用于测试的用户名,拿到用户名和密钥
需引用的jar包
commons-logging-1.1.1.jar
commons-httpclient-3.1.jar
commons-codec-1.4.jar
第一部分:SMS短信通编码接口
GBK编码发送接口地址:
http://gbk.sms.webchinese.cn/?Uid=本站用户名&Key=接口安全密码&smsMob=手机号码&smsText=短信容
UTF-8编码发送接口地址:
http://utf8.sms.webchinese.cn/?Uid=本站用户名&Key=接口安全密码&smsMob=手机号码&smsText=短信内容
获取短信数量接口地址(UTF8):
http://sms.webchinese.cn/web_api/SMS/?Action=SMS_Num&Uid=本站用户名&Key=接口安全密码
获取短信数量接口地址(GBK):
http://sms.webchinese.cn/web_api/SMS/GBK/?Action=SMS_Num&Uid=本站用户名&Key=接口安全密码
提示:HTTP调用URL接口时, 参数值必须URL编码后再调用
参数变量 |
说明 |
Gbk编码Url |
http://gbk.sms.webchinese.cn/ |
Utf-8编码Url |
http://utf8.sms.webchinese.cn/ |
Uid |
SMS短信通用户名(需注册) |
Key |
注册时填写的接口安全密码(可修改) |
smsMob |
目的手机号码(多个手机号请用半角逗号隔开) |
smsText |
短信内容,最多支持300个字,普通短信70个字/条,长短信64个字/条计费 |
提示:①、多个手机号请用半角,隔开如:13888888886,13888888887,1388888888 一次最多对100个手机发送。 ②、短信内容支持长短信,最多300个字,普通短信66个字/条,长短信64个字/条计费
短信发送后返回值 |
说明 |
-1 |
没有该用户账户 |
-2 |
密钥不正确 |
-3 |
短信数量不足 |
-11 |
该用户被禁用 |
-14 |
短信内容出现非法字符 |
-4 |
手机号格式不正确 |
-41 |
手机号码为空 |
-42 |
短信内容为空 |
-51 |
短信签名格式不正确接口签名格式为:【签名内容】 |
大于0 |
短信发送数量 |
|
注意:①、调用API接口,需登录中国网建SMS短信通平台,申请106网关发送,即发即到! ②、发送测试短信请勿输入:短信测试等词语,请直接提交您要发送的短信内容; ③、接口发送短信时请在内容后加签名:【XX公司或XX网名称】,否者会被屏蔽。 ④、短信签名可在用户平台平台上设置,也可以在短信内容后,直接加入。 |
JAVA语言发送短信核心代码:
- import java.io.UnsupportedEncodingException;
- import org.apache.commons.httpclient.Header;
- import org.apache.commons.httpclient.HttpClient;
- import org.apache.commons.httpclient.NameValuePair;
- import org.apache.commons.httpclient.methods.PostMethod;
-
- public class SendMsg_webchinese {
-
- public static void main(String[] args)throws Exception{
-
- HttpClient client = new HttpClient();
- PostMethod post = new PostMethod("http://gbk.sms.webchinese.cn");
- post.addRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=gbk");//在头文件中设置转码
- NameValuePair[] data ={
- new NameValuePair("Uid", "本站用户名"),
- new NameValuePair("Key", "接口安全密码"),
- new NameValuePair("smsMob","手机号码"),
- new NameValuePair("smsText","短信内容")
- };
- post.setRequestBody(data);
-
- client.executeMethod(post);
- Header[] headers = post.getResponseHeaders();
- int statusCode = post.getStatusCode();
- System.out.println("statusCode:"+statusCode);
-
- for(Header h : headers){
- System.out.println(h.toString());
- }
- String result = new String(post.getResponseBodyAsString().getBytes("gbk"));
- System.out.println(result);
-
- post.releaseConnection();
- }
- }