
一、本文目的
- 大部分平台都有一个接入发送短信验证码、通知短信的需求。虽然市场上大部分平台的接口都只是一个非常普通的HTTP-GET请求,但终归有需要学习和借鉴使用的朋友。
- 本文的初衷是主要提供学习便利,方便初学者学习简单的http接口对接实现,由于各大短信通道服务商API参数基本相同,本Demo可以适当减少开发者对接短信通道接口的开发成本,提供参考。
- 若有朋友不想从文章中复制源码,需要直接下载,源码已上传至GitHub。
- GitHub传送门:https://github.com/yzchen0o0/demo-sms
二、发送短信接口请求
https://{{url}}?appKey={{app_key}}&appSecret={{app_secret}}&phones={{mobile}}&content=【{{sign}}】{{content}}
https://xxxyun.com/sendsms?appKey=aaaaa&appSecret=bbbb&phones=13888888888&content=【某云】您的验证码是:666666
三、参数描述
参数名 | 说明 |
---|
url | 请求地址 |
app_key | 客户在供应商注册的唯一key码 |
app_secret | 客户在供应商注册的唯一secret码 |
mobile | 接收短信的用户手机号 |
sign | 短信内容的产品名称 |
四、各开发语言实现
1、Java
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class SmsUtil {
private static final String HOST = "https://api.zhuanxinyun.com/api/v2/sendSms.json";
private static final String SIGN = "【签名】";
private static final String APP_KEY = "app_key";
private static final String APP_SECRET = "app_secret";
public static void main(String[] args) {
String mobile = "18566770000";
String code = "666666";
String body = sendSmsCode(mobile, code);
System.out.println(body);
}
public static String sendSmsCode(String mobile, String code) {
StringBuffer content = new StringBuffer().append("验证码:" ).append(code).append(",如非本人操作请忽略。");
return sendSms(mobile, content.toString());
}
public static String sendSms(String mobile, String content) {
StringBuffer url = new StringBuffer().append(HOST).append("?appKey=").append(APP_KEY).append("&appSecret=")
.append(APP_SECRET).append("&phones=").append(mobile).append("&content=").append(SIGN).append(content);
Request request = new Request.Builder().url(url.toString()).get().build();
OkHttpClient client = new OkHttpClient();
try {
Response response = client.newCall(request).execute();
return response.body().string();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
2、php
<?php
$host = "https://api.zhuanxinyun.com/api/v2/sendSms.json";
$sign = "【签名】";
$app_key = "app_key";
$app_secret = "app_secret";
$code = "6666";
$phones = "18088888888";
$content = "本次验证码是".code.",如非本人操作请忽略。";
$uri = $host."?appKey=".$app_key."&appSecret=".$app_secret."&phones=".$phones."&content=".$sign.$content;
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$contents = curl_exec($ch);
curl_close($ch);
var_dump($contents);
?>
3、.Net
using System;
using System.IO;
using System.Net;
using System.Text;
namespace SmsCode
{
class Program
{
private static String HOST = "https://api.zhuanxinyun.com/api/v2/sendSms.json";
private static String SIGN = "【签名】";
private static String APP_KEY = "app_key";
private static String APP_SECRET = "APP_SECRET";
static void Main(string[] args)
{
string mobile = "手机号";
string code = "内容";
string body = sendSmsCode(mobile, code);
Console.WriteLine(body);
}
public static String sendSmsCode(String mobile, String code)
{
StringBuilder content = new StringBuilder().Append("验证码:").Append(code).Append(",如非本人操作请忽略。");
return sendSms(mobile, content.ToString());
}
public static string sendSms(String mobile, String content)
{
string msg = string.Empty;
StringBuilder url = new StringBuilder().Append(HOST).Append("?appKey=").Append(APP_KEY).Append("&appSecret=")
.Append(APP_SECRET).Append("&phones=").Append(mobile).Append("&content=").Append(SIGN).Append(content);
try
{
msg = Request_GET(url.ToString(), "UTF-8");
}
catch (Exception ex)
{
msg=ex.Message;
}
return msg;
}
public static string Request_GET(string urlString, string encoding)
{
HttpWebRequest httpWebRequest = null;
HttpWebResponse httpWebResponse = null;
Stream stream = null;
StreamReader streamReader = null;
string result = string.Empty;
try
{
httpWebRequest = (WebRequest.Create(urlString) as HttpWebRequest);
httpWebRequest.Method = "GET";
httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
stream = httpWebResponse.GetResponseStream();
streamReader = new StreamReader(stream, Encoding.GetEncoding(encoding));
result = streamReader.ReadToEnd();
}
catch (SystemException ex)
{
result = "err:" + ex.Message;
}
finally
{
if (httpWebRequest != null)
{
httpWebRequest.Abort();
}
if (httpWebResponse != null)
{
httpWebResponse.Close();
}
if (stream != null)
{
stream.Close();
}
if (streamReader != null)
{
streamReader.Close();
}
}
return result;
}
}
}
五、返回值
{"errorCode":"000000","errorMsg":"提交成功"}
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xml>
<errorCode>000000</errorCode>
<errorMsg>提交成功</errorMsg>
</xml>
技术交流

My Blog
blog.guijianpan.com
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程