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

相关推荐
维克兜率天2 小时前
【维克】RSI相对强弱指数:判断多空力量的温度计
前端
蜡笔小谢8 小时前
DeepSeek Harness Java 实战笔记(五)|插件、MCP 与 Skill
java·笔记·ai·agent·deepseek·harness
FRing039 小时前
vue2干货总结12(loading效果、全局前置导航守卫、搜索历史管理)
开发语言·前端·javascript·vue.js·vue2
wefg19 小时前
【项目】C++ 基于 Vibe Coding 的 OJ 平台
开发语言·c++
YIAN9 小时前
我踩完 DeepAgents 记忆模块的坑,才搞懂长期记忆和摘要不是一回事
前端·javascript·agent
唐老板10 小时前
Spring AI 2.0 只支持 Boot 4
java
YIAN10 小时前
用 Deep Agents 复现一个深度调研助手:从主 Agent、子 Agent 到 Skill
javascript·agent
map_3d_vis12 小时前
百度地图 JSAPI 4.0 是什么:版本说明与能力概览
前端·javascript·webgl·地图·百度地图
码云之上12 小时前
微信小程序设备指纹实践,删了重装还能认出这台手机吗?
前端·安全·微信小程序
JavaGuide12 小时前
刚刚,GLM-5.4 正式发布,实测来了:看图用 Blender 建凉亭、做技术视频,再画一辆真能传动的自行车
前端·后端