阿里云发送手机短信

1、登录注册阿里云账号 在搜索框中输入106三网短信

2、找到有0元测试套餐的商品

3、选择0元5条的套餐

4、购买成功后右上角买家中心-管理控制台

5、打开后可以看到刚刚下单的短信API的APPCode(后面会用到)

6、POM依赖导入

        <!--AlibabaPhone--><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.15</version></dependency><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.2.1</version></dependency><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpcore</artifactId><version>4.2.1</version></dependency><dependency><groupId>commons-lang</groupId><artifactId>commons-lang</artifactId><version>2.6</version></dependency><dependency><groupId>org.eclipse.jetty</groupId><artifactId>jetty-util</artifactId><version>9.3.7.v20160115</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.5</version><scope>test</scope></dependency><!--AlibabaPhone-->

7、HttpUtil(固定的)

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
​
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
​
import org.apache.commons.lang.StringUtils;
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.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
/*** @author: tobed* @date: 2022/10/21* @time: 14:40*/
public class HttpUtils {
​/*** get** @param host* @param path* @param method* @param headers* @param querys* @return* @throws Exception*/public static HttpResponse doGet(String host, String path, String method,Map<String, String> headers,Map<String, String> querys)throws Exception {HttpClient httpClient = wrapClient(host);
​HttpGet request = new HttpGet(buildUrl(host, path, querys));for (Map.Entry<String, String> e : headers.entrySet()) {request.addHeader(e.getKey(), e.getValue());}
​return httpClient.execute(request);}
​/*** post form** @param host* @param path* @param method* @param headers* @param querys* @param bodys* @return* @throws Exception*/public static HttpResponse doPost(String host, String path, String method,Map<String, String> headers,Map<String, String> querys,Map<String, String> bodys)throws Exception {HttpClient httpClient = wrapClient(host);
​HttpPost request = new HttpPost(buildUrl(host, path, querys));for (Map.Entry<String, String> e : headers.entrySet()) {request.addHeader(e.getKey(), e.getValue());}
​if (bodys != null) {List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
​for (String key : bodys.keySet()) {nameValuePairList.add(new BasicNameValuePair(key, bodys.get(key)));}UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairList, "utf-8");formEntity.setContentType("application/x-www-form-urlencoded; charset=UTF-8");request.setEntity(formEntity);}
​return httpClient.execute(request);}
​/*** Post String** @param host* @param path* @param method* @param headers* @param querys* @param body* @return* @throws Exception*/public static HttpResponse doPost(String host, String path, String method,Map<String, String> headers,Map<String, String> querys,String body)throws Exception {HttpClient httpClient = wrapClient(host);
​HttpPost request = new HttpPost(buildUrl(host, path, querys));for (Map.Entry<String, String> e : headers.entrySet()) {request.addHeader(e.getKey(), e.getValue());}
​if (StringUtils.isNotBlank(body)) {request.setEntity(new StringEntity(body, "utf-8"));}
​return httpClient.execute(request);}
​/*** Post stream** @param host* @param path* @param method* @param headers* @param querys* @param body* @return* @throws Exception*/public static HttpResponse doPost(String host, String path, String method,Map<String, String> headers,Map<String, String> querys,byte[] body)throws Exception {HttpClient httpClient = wrapClient(host);
​HttpPost request = new HttpPost(buildUrl(host, path, querys));for (Map.Entry<String, String> e : headers.entrySet()) {request.addHeader(e.getKey(), e.getValue());}
​if (body != null) {request.setEntity(new ByteArrayEntity(body));}
​return httpClient.execute(request);}
​/*** Put String* @param host* @param path* @param method* @param headers* @param querys* @param body* @return* @throws Exception*/public static HttpResponse doPut(String host, String path, String method,Map<String, String> headers,Map<String, String> querys,String body)throws Exception {HttpClient httpClient = wrapClient(host);
​HttpPut request = new HttpPut(buildUrl(host, path, querys));for (Map.Entry<String, String> e : headers.entrySet()) {request.addHeader(e.getKey(), e.getValue());}
​if (StringUtils.isNotBlank(body)) {request.setEntity(new StringEntity(body, "utf-8"));}
​return httpClient.execute(request);}
​/*** Put stream* @param host* @param path* @param method* @param headers* @param querys* @param body* @return* @throws Exception*/public static HttpResponse doPut(String host, String path, String method,Map<String, String> headers,Map<String, String> querys,byte[] body)throws Exception {HttpClient httpClient = wrapClient(host);
​HttpPut request = new HttpPut(buildUrl(host, path, querys));for (Map.Entry<String, String> e : headers.entrySet()) {request.addHeader(e.getKey(), e.getValue());}
​if (body != null) {request.setEntity(new ByteArrayEntity(body));}
​return httpClient.execute(request);}
​/*** Delete** @param host* @param path* @param method* @param headers* @param querys* @return* @throws Exception*/public static HttpResponse doDelete(String host, String path, String method,Map<String, String> headers,Map<String, String> querys)throws Exception {HttpClient httpClient = wrapClient(host);
​HttpDelete request = new HttpDelete(buildUrl(host, path, querys));for (Map.Entry<String, String> e : headers.entrySet()) {request.addHeader(e.getKey(), e.getValue());}
​return httpClient.execute(request);}
​private static String buildUrl(String host, String path, Map<String, String> querys) throws UnsupportedEncodingException {StringBuilder sbUrl = new StringBuilder();sbUrl.append(host);if (!StringUtils.isBlank(path)) {sbUrl.append(path);}if (null != querys) {StringBuilder sbQuery = new StringBuilder();for (Map.Entry<String, String> query : querys.entrySet()) {if (0 < sbQuery.length()) {sbQuery.append("&");}if (StringUtils.isBlank(query.getKey()) && !StringUtils.isBlank(query.getValue())) {sbQuery.append(query.getValue());}if (!StringUtils.isBlank(query.getKey())) {sbQuery.append(query.getKey());if (!StringUtils.isBlank(query.getValue())) {sbQuery.append("=");sbQuery.append(URLEncoder.encode(query.getValue(), "utf-8"));}}}if (0 < sbQuery.length()) {sbUrl.append("?").append(sbQuery);}}
​return sbUrl.toString();}
​private static HttpClient wrapClient(String host) {HttpClient httpClient = new DefaultHttpClient();if (host.startsWith("https://")) {sslClient(httpClient);}
​return httpClient;}
​private static void sslClient(HttpClient httpClient) {try {SSLContext ctx = SSLContext.getInstance("TLS");X509TrustManager tm = new X509TrustManager() {public X509Certificate[] getAcceptedIssuers() {return null;}public void checkClientTrusted(X509Certificate[] xcs, String str) {
​}public void checkServerTrusted(X509Certificate[] xcs, String str) {
​}};ctx.init(null, new TrustManager[] { tm }, null);SSLSocketFactory ssf = new SSLSocketFactory(ctx);ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);ClientConnectionManager ccm = httpClient.getConnectionManager();SchemeRegistry registry = ccm.getSchemeRegistry();registry.register(new Scheme("https", 443, ssf));} catch (KeyManagementException ex) {throw new RuntimeException(ex);} catch (NoSuchAlgorithmException ex) {throw new RuntimeException(ex);}}
}
6、java中main代码实例
public static void main(String[] args) {String host = "https://gyytz2.market.alicloudapi.com";String path = "/sms/smsSendLong";String method = "POST";String appcode = "你自己的AppCode"; //这里是刚刚说到的控制台中的订单APPCodeMap<String, String> headers = new HashMap<String, String>();//最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105headers.put("Authorization", "APPCODE " + appcode);Map<String, String> querys = new HashMap<String, String>();querys.put("mobile", "mobile"); //这里是要发送的手机号querys.put("param", "**code**:12345,**minute**:5");querys.put("smsSignId", "2e65b1bb3d054466b82f0c9d125465e2");querys.put("templateId", "908e94ccf08b4476ba6c876d13f084ad");Map<String, String> bodys = new HashMap<String, String>();
​
​try {/*** 重要提示如下:* HttpUtils请从* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java* 下载** 相应的依赖请参照* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml*/HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);System.out.println(response.toString());//获取response的body//System.out.println(EntityUtils.toString(response.getEntity())); //这里是返回值} catch (Exception e) {e.printStackTrace();}}

打开返回值后 正确的返回值是

{"msg": "成功", "smsid": "16565614329364584123421",  //批次号。可通过该ID查询发送状态或者回复短信。API接口可联系客服获取。"code": "0","balance": "1234"  //账户剩余次数
}

到这里就结束了

如果想要有短信模板的需要付费

这里只是五次免费试用哦!

阿里云发送手机短信 (呆瓜教学)相关推荐

  1. 基于阿里云的手机短信验证码和注册校验逻辑

    基于阿里云的手机短信验证码demo实现 1. 环境依赖 2. 页面表单 html 3. 校验与短信 js 4. 工具类 SmsUtils 5. 资源调用 Servlet 阿里云的短信平台:http:/ ...

  2. java使用阿里云发送通知短信

    首先在阿里云短信平台找到这几个参数对应的信息 阿里云短信秘钥 aliyun: accessKeyId: accessKeySecret: #短信签名,可以在阿里云短信控制台查找 messageSign ...

  3. 容联云发送手机短信验证码

    首先在根目录下定义全局使用的连接容联云py的文件 下载SDK pip install ronglian_sms_sdk 在文件中导入 ↓ from ronglian_sms_sdk import Sm ...

  4. 阿里云的手机短信验证

    控制层 //发送验证码@RequestMapping("/sendVerifyCode")@ResponseBodypublic ResponseEntity sendVerify ...

  5. 如何发送手机短信验证码

    文章目录 阿里云短信业务实战教程 1.阿里云平台的使用 2.创建用户组及用户并添加权限 3.添加短信签名和短信模板并充值费用 4.开发工具进行代码部分(这里使用IDEA) 阿里云短信业务实战教程 手机 ...

  6. 【微信小程序 - 工作实战分享】1.微信小程序发送手机短信验证码(阿里云)

    发送手机短信验证码 前言 一. 准备工作 二. 配置 三. 实战代码(仅仅是后台代码,前端传入手机号) 总结 前言 在网站和移动应用中利用短信验证码进行信息确认是最常用的验证手段.随着短信验证码的技术 ...

  7. 健康管理系统第六天(移动端开发之体检预约_经典五表联查_调用阿里云提供的短信服务进行短信验证码发送)

    一.移动端开发 1.移动端开发方式 随着移动互联网的兴起和手机的普及,目前移动端应用变得愈发重要,成为了各个商家的必争之地.例如,我们可以使用手机购物.支付.打车.玩游戏.订酒店.购票等, 以前只能通 ...

  8. 使用阿里云提供的短信服务发送短信(个人版)

    本人最近需要实现一个注册发短信验证码的功能,找了几家短信服务平台,如腾讯云,云片网等,发现他们都没有提供给用户个人的短信服务权限,申请短信签名等都需要有企业等相关证明,最后找到了阿里云的短信服务平台, ...

  9. JAVA + 阿里云 实现单个短信发送 和 批量短信发送(直接拷贝就能使用)

    JAVA + 阿里云 实现单个短信发送 和 批量短信发送 一.阿里云官网相关操作 1.1 秘钥获取 1.2 签名申请 在短信服务中,找到国内消息-签名管理-添加签名, 并等待签名审核通过 1.2 模板 ...

最新文章

  1. 我想成为一名计算机管理员英语作文,我想成为一名老师英语作文4篇
  2. 三维人脸识别研究进展综述(附pdf)
  3. 华丽丽的GridLayout-使用案例
  4. Ubuntu18.04 JDK8
  5. vmos框架_VMOS虚拟大师独立的安卓虚拟机系统【安卓】
  6. Shell编程之变量赋值和引用
  7. vue中生产模式和调试模式_为什么在生产中进行调试是如此诱人?
  8. python学习 第一篇 基础
  9. centos8 apache php,centos6.8安装php7 for Apache2
  10. HTML文档的三大构成元素,构成基础结课小结 为什么三大构成是设计的基础课程...
  11. hadoop 开启防火墙_hadoop上搭建hive
  12. 646. Maximum Length of Pair Chain 最长的链条长度
  13. android Fragment详解三:实现Fragment的界面
  14. zlib再windows下的编译
  15. acm运行时错误_计人即讯|ACM纳新赛
  16. android 各国语言对应的缩写
  17. Salesforce基础名词
  18. 算法竞赛宝典 递归算法 地盘划分
  19. Cadence Allegro使用过程中出现的常见问题-原理图和PCB
  20. DCloud UniAPP Android 蓝牙连接ESCPOS打印机

热门文章

  1. eml格式是什么格式文件?如何打开eml文件?
  2. excel 转 数据库python_Python实战 :2017国考职业表excel转数据库,再查询生成excel
  3. 3款比较优秀的神秘工具推荐
  4. Anaconda的下载和使用
  5. 建设 TiDB 自动化平台:转转 DBA 团队实践
  6. 北大计算机博士后岗位需求,北京大学戴志飞课题组招聘博士后及科研助理
  7. 《淘宝交付之道》出版!大淘宝技术36个月匠心之作
  8. 蓝桥杯 15决赛 A2 四阶幻方(剪枝)
  9. fetchall()
  10. centos6搭建私有云