Java实现手机短信验证码(互亿无线)

互亿无线

互亿无线是一家提供电信类增值服务插件以及其他相关插件的公司,是中国移动、中国联通、中国电信三大运营商的战略合作伙伴与工信部认定的电信增值业务服务商。公司旗下运营三大业务平台:数字奖励营销活动平台、应用短信平台、营销短信平台。

官网:短信api_短信接口api_短信接口验证码_短信验证码API_短信通知api接口_互亿无线

实现方法

1、pom.xml文件导包

XML 复制代码
        <!--    短信    --> 
        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency>
        <dependency>
            <groupId>commons-httpclient</groupId>
            <artifactId>commons-httpclient</artifactId>
            <version>3.0</version>
        </dependency>
 
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.13</version>
        </dependency>

2、创建发送短信工具类

一般把这个类放到util工具目录下(我自己是在若依框架下的项目)

java 复制代码
package com.ruoyi.common.utils;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.io.IOException;


public class SendSmsUtil {
    private static final String URL = "http://106.ihuyi.com/webservice/sms.php?method=Submit"; //国内请求路径
    private static final String APPID = "******(这里填写自己的APPID)";
    private static final String APIKEY = "*****(这里填写自己的APIKEY)";

    public static String SMS(String number) throws HttpException, IOException{

        HttpClient client = new HttpClient();
        PostMethod method = new PostMethod(URL);

        client.getParams().setContentCharset("utf-8");
        method.setRequestHeader("ContentType","application/x-www-form-urlencoded;charset=utf-8");
        //随机编号
        int mobile_code = (int)((Math.random()*9+1)*100000);

        String content = new String("您的验证码是:" + mobile_code + "。请不要把验证码泄露给其他人。");

        NameValuePair[] data = {//提交短信
                new NameValuePair("account",APPID ), //查看用户名 登录用户中心->验证码通知短信>产品总览->API接口信息->APIID
                new NameValuePair("password", APIKEY), //查看密码 登录用户中心->验证码通知短信>产品总览->API接口信息->APIKEY
                new NameValuePair("mobile", number),
                new NameValuePair("content", content),
        };
        method.setRequestBody(data);

        try {
            client.executeMethod(method);

            String SubmitResult =method.getResponseBodyAsString();


            Document doc = DocumentHelper.parseText(SubmitResult);
            Element root = doc.getRootElement();

            String code = root.elementText("code");
            String msg = root.elementText("msg");
            String smsid = root.elementText("smsid");

            System.out.println("code"+code);
            System.out.println("msg"+msg);
            System.out.println("smsid"+smsid);
            System.out.println("mo"+mobile_code);

            if("2".equals(code)){
                System.out.println("短信提交成功");
                System.out.println(mobile_code);
                return mobile_code+"";
            }

        } catch (HttpException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "";
    }
}

注意!!!

在写工具类之前,要先去互亿无线官网注册账号,获取到你个人的一个APPID和APIKEY。

3、Contriller控制层调用工具类

java 复制代码
//获取验证码
@RequestMapping("/sendCode")
    public void sendCode(HttpServletRequest req, PrintWriter pw)
            throws ServletException, IOException {
        String number=req.getParameter("number");
        String strCode= SendSmsUtil.SMS(number);
        req.getSession().setAttribute("code", strCode);
        System.out.println("strCode"+strCode);
        if(strCode!=""){
            pw.write("success");
        }
    }

4、前端代码

html 复制代码
//界面显示
电话号码: <input type="text" id="number" name="number"  placeholder="电话" />
验证码: <input type="text" id="captcha" name="captcha"   />
<input type="button" value="获取验证码" id="update" onclick="sendSms();"/>
javascript 复制代码
function sendSms() {
				var number = $("#number").val();
				$.post("/html/sendCode",{number:number},function (data) {
					if (data == "success"){
						var second = 60;
						console.log("发送成功");
					}
				})
			}

可以参考这两篇文章:

Java实现手机发送短信验证码_java发送短信验证码-CSDN博客

Java短信验证码-互亿无线_互亿无线 工具类-CSDN博客

相关推荐
信奥卷王43 分钟前
[GESP202503 五级] 原根判断
java·数据结构·算法
小咕聊编程1 小时前
【含文档+源码】基于SpringBoot的过滤协同算法之网上服装商城设计与实现
java·spring boot·后端
Zz_waiting.1 小时前
Spring 原理
java·spring·spring自动管理
Predestination王瀞潞4 小时前
IO操作(Num22)
开发语言·c++
瓯雅爱分享5 小时前
Java+Vue构建的采购招投标一体化管理系统,集成招标计划、投标审核、在线竞价、中标公示及合同跟踪功能,附完整源码,助力企业实现采购全流程自动化与规范化
java·mysql·vue·软件工程·源代码管理
光影少年5 小时前
angular生态及学习路线
前端·学习·angular.js
宋恩淇要努力5 小时前
C++继承
开发语言·c++
沿着路走到底6 小时前
python 基础
开发语言·python
記億揺晃着的那天7 小时前
Vue + Element UI 表格自适应高度如何做?
javascript·vue.js·ui
无尽夏_7 小时前
HTML5(前端基础)
前端·html·html5