使用JAVA调用短信接口发送验证码短信
JAVA对接验证码短信接口DEMO示例
<p><img title="使用java调用短信接口发送单条短信(图1)" alt="使用java调用短信接口发送单条短信(图1)" src="/uploads/allimg/20240523/1-240523205T0531.jpeg"/></p>
<p>注册、登录、找回密码这些场景都离不开短信验证码。这篇文章讲怎么用 Java 调用短信接口,发送单条短信。接口选用智慧云信的 <code>https://api.3yit.com/api/send-sms-single</code>,支持 POST 请求,返回 JSON 格式的结果。</p>
<h2>接口地址与调用方式</h2>
<p>接口地址为 <code>https://api.3yit.com/api/send-sms-single</code>,请求方式为 POST,请求头 <code>Content-Type</code> 指定为 <code>application/x-www-form-urlencoded</code>。发送请求时需传入 <code>sp_id</code>、<code>mobile</code>、<code>content</code> 等参数,接口返回的 JSON 对象里包含发送状态、消息 ID 等信息。</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>Java 代码实现</h2>
<p>发 POST 请求需要用到 HTTP 客户端库,这里选用 Apache 的 HttpClient,它的 HTTP 协议处理能力比较完整。下面是完整的示例代码:</p>
<pre><code>import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class SMSSender {
public static void main(String[] args) {
String apiUrl = "https://api.3yit.com/api/send-sms-single";
String spId = "5xxxxx"; // 替换为实际的产品sp_id
String mobile = "176xxxxxxxx"; // 替换为实际的手机号
String content = "【签名】验证码123"; // 替换为实际的短信内容
String signature = "a92569901189fxxxxxxxx957c096c432e2d47635"; // 如果使用签名验证,替换为实际的签名
// String password = md5("your_password_here"); // 如果使用密码验证,替换为实际密码的md5值
try {
sendSMS(apiUrl, spId, mobile, content, signature, null); // 如果使用密码,则传入password,将signature置为null
} catch (IOException e) {
e.printStackTrace();
}
}
public static void sendSMS(String apiUrl, String spId, String mobile, String content, String signature, String password) throws IOException {
HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(apiUrl);
List&lt;NameValuePair&gt; params = new ArrayList&lt;&gt;();
params.add(new BasicNameValuePair("sp_id", spId));
params.add(new BasicNameValuePair("mobile", mobile));
params.add(new BasicNameValuePair("content", content));
if (signature != null) {
params.add(new BasicNameValuePair("signature", signature));
} else if (password != null) {
params.add(new BasicNameValuePair("password", password));
}
// 如果需要,可以添加其他参数,如ext
post.setEntity(new UrlEncodedFormEntity(params));
post.setHeader("Content-Type", "application/x-www-form-urlencoded");
HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity();
if (entity != null) {
String result = EntityUtils.toString(entity);
System.out.println(result); // 打印返回的JSON结果
// 这里可以解析返回的JSON,获取发送状态、消息ID等信息
// ...
}
}
// 这里可以添加md5加密方法,如果需要的话
// ...
}</code></pre>
<h2>注意事项</h2>
<ul><li>实际使用时,把 <code>sp_id</code>、<code>mobile</code>、<code>content</code> 等参数替换成自己的值。</li><li>选择签名验证时,签名算法要与接口文档一致,把签名值替换到代码中对应的变量上。</li><li>选择密码验证时,把密码做 MD5 加密,再把加密后的值替换到对应变量上。</li><li>接口返回后,建议解析 JSON 中的 <code>code</code>、<code>msg</code>、<code>msg_id</code> 字段,根据状态码做相应处理。</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/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/285.html" title="JSP短信接口教程">JSP</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
本文主题:使用JAVA调用短信接口发送验证码短信
本文围绕「使用JAVA调用短信接口发送验证码短信」展开,结合智慧云信平台在短信接口API场景下的产品能力与客户接入经验整理。不同国家/地区的 Sender ID、模板审核、计费方式和合规要求可能变化,实际发送前建议以当前通道审核与项目沟通结果为准。