使用JSP调用短信接口发送验证码短信教程
JSP对接验证码短信接口DEMO示例
<p><img title="使用JSP调用短信接口发送验证码短信教程(图1)" alt="使用JSP调用短信接口发送验证码短信教程(图1)" src="/uploads/allimg/20240524/1-2405241931134L.jpeg"/></p>
<p>注册新账号、找回密码、支付验证,这些场景都会用到短信验证码。这篇文章讲怎么在 JSP(Java Server Pages)环境下调用短信接口,把验证码短信发出去。</p>
<h2>准备工作</h2>
<p>开始之前,先确认已经拿到短信服务商(如智慧云信)的开发者账号,并在文档里找到自己的 <code>sp_id</code> 和 <code>password</code>(或 <code>signature</code>)。</p>
<h2>获取接口参数</h2>
<p>登录智慧云信官网,选择对应的产品(如【验证码】),在上方选项卡中点击【开发者】→【HTTP开发文档】,就能看到具体的 API 方法名称,以及各参数的 key 和对应值,通常包括 <code>sp_id</code>、<code>mobile</code>、<code>content</code>、<code>password</code> 等。各参数含义如下:</p>
<ul><li><code>sp_id</code>:产品编号,全局唯一,一般由 6 位数字组成。</li><li><code>mobile</code>:接收短信的手机号码。</li><li><code>content</code>:短信内容,包含签名和正文。例如“【智慧云信】您的验证码是456790,请妥善保管”,其中【智慧云信】是签名,需在签名报备中申请;后面的提示语是短信模板,需在模板报备中申请,审核通过后才能发送。</li><li><code>password</code>:用 SP_ID 密码经 MD5 加密后生成的接口密码(非登录密码),由 32 位随机字符串组成。</li></ul>
<h2>编写 JSP 页面</h2>
<p>在 JSP 页面里用 Java 代码发送 HTTP POST 请求到短信接口,下面是示例:</p>
<pre><code>&lt;%@ page import="java.io.*,java.net.*,java.util.HashMap,java.util.Map" %&gt;
&lt;%
// 设置请求参数
String spId = "5xxxxx"; // 从开发者文档中获取
String mobile = "176xxxxxxxx"; // 接收验证码的手机号
String content = "【签名】验证码123"; // 短信内容,其中验证码需动态生成
String password = "xxxxxxxxxxx"; // 32位MD5处理后的密码,从开发者文档中获取
// 签名和密码二选一,这里以密码为例
// 构造请求URL和参数
String url = "https://api.3yit.com/api/send-sms-single";
Map&lt;String, String&gt; params = new HashMap&lt;&gt;();
params.put("sp_id", spId);
params.put("mobile", mobile);
params.put("content", content);
params.put("password", password);
// 发送POST请求
String response = sendPostRequest(url, params);
// 解析返回结果
JSONObject jsonObject = new JSONObject(response); // 假设你已经导入了JSONObject库
int code = jsonObject.getInt("code");
String msg = jsonObject.getString("msg");
String msgId = jsonObject.getString("msg_id");
// 输出结果到页面
out.println("发送结果:&lt;br&gt;");
out.println("Code: " + code + "&lt;br&gt;");
out.println("Msg: " + msg + "&lt;br&gt;");
out.println("Msg ID: " + msgId + "&lt;br&gt;");
%&gt;
&lt;%!
// 发送POST请求的方法
public String sendPostRequest(String url, Map&lt;String, String&gt; params) {
StringBuilder postData = new StringBuilder();
for (Map.Entry&lt;String, String&gt; param : params.entrySet()) {
if (postData.length() != 0) postData.append('&amp;');
postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
postData.append('=');
postData.append(URLEncoder.encode(param.getValue(), "UTF-8"));
}
byte[] postDataBytes = postData.toString().getBytes("UTF-8");
HttpURLConnection conn = null;
try {
URL obj = new URL(url);
conn = (HttpURLConnection) obj.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
conn.setDoOutput(true);
try (OutputStream os = conn.getOutputStream()) {
os.write(postDataBytes);
}
try (BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()))) {
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
return response.toString();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (conn != null) {
conn.disconnect();
}
}
return null;
}
%&gt;</code></pre>
<h2>注意事项</h2>
<ul><li>验证码要在业务端动态生成,保证唯一性和安全性,不要把写死的验证码发出去。</li><li>防止短信轰炸:对发送频率做限制,并加黑名单功能。</li><li>签名和密码按服务商要求二选一,本示例以密码为例,签名算法也是常见的验证方式。</li><li>处理返回结果时,根据 <code>code</code> 的值做错误处理,不要只判断 <code>msg</code> 是否为 success。</li></ul>
<p><strong>其他语言教程:</strong><a href="https://www.3yit.com/smsapi/262.html" title="PHP短信接口教程">PHP</a> · <a href="https://www.3yit.com/smsapi/282.html" title="Java短信接口教程">Java</a> · <a href="https://www.3yit.com/smsapi/283.html" title="ASP短信接口教程">ASP</a> · <a href="https://www.3yit.com/smsapi/284.html" title="ASP.NET(C#)短信接口教程">ASP.NET(C#)</a> · <a href="https://www.3yit.com/smsapi/286.html" title="Node.js短信接口教程">Node.js</a> · <a href="https://www.3yit.com/smsapi/298.html" title="Delphi短信接口教程">Delphi</a> · <a href="https://www.3yit.com/smsapi/300.html" title="Go短信接口教程">Go</a> · <a href="https://www.3yit.com/smsapi/301.html" title="Python短信接口教程">Python</a> · <a href="https://www.3yit.com/smsapi/302.html" title="Ruby短信接口教程">Ruby</a> · <a href="https://www.3yit.com/smsapi/303.html" title="Shell短信接口教程">Shell</a> · <a href="https://www.3yit.com/smsapi/304.html" title="VB短信接口教程">VB</a> · <a href="https://www.3yit.com/smsapi/305.html" title="C++短信接口教程">C++</a> · <a href="https://www.3yit.com/smsapi/306.html" title="C短信接口教程">C</a>。更多信息见<a href="https://www.3yit.com/smsapi/613.html" title="短信API接入指南">短信API接入指南</a>。</p>
<p>代码里的 API 地址、账号和密码替换成你自己的就能跑通。如果还没有账号,去智慧云信官网注册一个,注册就能免费测试,验证码、通知、营销短信都支持。</p>
内容可信度说明
作者:智慧云信通信技术团队
技术审核:国际短信与号码检测产品组
最后更新:2026-08-13
本文主题:使用JSP调用短信接口发送验证码短信教程
本文围绕「使用JSP调用短信接口发送验证码短信教程」展开,结合智慧云信平台在短信接口API场景下的产品能力与客户接入经验整理。不同国家/地区的 Sender ID、模板审核、计费方式和合规要求可能变化,实际发送前建议以当前通道审核与项目沟通结果为准。