阿里云发送手机短信

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. Java程序后台运行,即使关掉Putty终端
  2. 【深度学习笔记】ROC曲线 vs Precision-Recall曲线
  3. 一分钟教你用Excel从统计局抓数据!
  4. 运动基元_发现大量Java基元集合处理
  5. c# WebApi之接口返回类型详解
  6. 修改2440里面的FriendlyARM
  7. uc浏览器将在印度推出电商服务
  8. 5年之后,产品经理,没了?
  9. 2017.9.28 约数研究 思考记录
  10. scrapy Crawl_spider
  11. ftp信息或服务器信息,服务器:FTP报错信息怎么办
  12. Java 并发编程之美:线程相关的基础知识
  13. 【EXCEL批量查询手机号归属地小技巧】很多网友想看excel怎么批量查询手机号归属地,今天它来了
  14. 《遥感原理与应用》总结—遥感传感器及成像原理
  15. TX-LCN分布式事务之LCN模式
  16. 什么是软路由,软路由和普通路由器有何区别
  17. 使用OpenCV合成训练图片,同时生成labelme兼容格式的标注文件
  18. zlog日志系统开发中遇到的问题(2)
  19. rr rom Android6,RR ROM 手把手教学刷入和体验
  20. Sheldon Numbers (暴力枚举)

热门文章

  1. 【HTML5系列一】:HTML初识
  2. 【计算机毕业设计】144高校办公室行政事务管理系统
  3. jexus 部署c#项目500异常解决办法
  4. 蓝牙Mesh 灯控案例
  5. <Essential C++学习>入门
  6. sde10 oracle11g,Linux下安装Arcsde10
  7. 程序员如何在996高压之下,求得一线生机?
  8. 数据湖10:新型大数据解决方案,数据湖如何建设?
  9. win10系统下制作win10x64pe教程
  10. UOJ Round #20 T1 A. 【UR #20】跳蚤电话(组合数+树形DP)