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博客

相关推荐
水云桐程序员3 小时前
C++可以写手机应用吗
开发语言·c++·智能手机
测试员周周3 小时前
【AI测试智能体】为什么传统测试方法对智能体失效?
开发语言·人工智能·python·功能测试·测试工具·单元测试·测试用例
kyriewen3 小时前
百度用6%成本碾压硅谷?中国AI把性价比玩明白了
前端·百度·ai编程
kyriewen4 小时前
你还在手动敲命令部署?GitHub Actions 让你 push 即上线,摸鱼时间翻倍
前端·面试·github
RSTJ_16254 小时前
PYTHON+AI LLM DAY THREETY-NINE
开发语言·人工智能·python
想学习java初学者4 小时前
SpringBoot整合Vertx-Mqtt多租户(优化版)
java·spring boot·后端
AC赳赳老秦5 小时前
政企内网落地:OpenClaw 离线环境深度适配方案,无外网场景下本地化模型对接与全功能使用
java·大数据·运维·python·自动化·deepseek·openclaw
赏金术士5 小时前
Kotlin 从入门到进阶 之函数模块(核心基础)(二)
android·开发语言·kotlin
weixin_449173655 小时前
在 Java 中,‌线程安全的 List‌ 主要有以下几种实现方式,它们的效率取决于具体的使用场景(尤其是读写比例):
java·线程安全的list
砚底藏山河5 小时前
股票数据API接口:如何获取股票历历史分时KDJ数据
java·python·maven