一、在阿里云云市场购买试用的短信服务


  1. 打开阿里云,进入云市场
  2. 找到适合自己的商品进行购买
    本人使用以下商品:短信服务

    点击购买,测试完成后,联系客服。配置你的专属短信模版
    本人接下来演示 测试模版2:

二、根据演示实例进行短信演示



将该演示代码复制进项目测试:

  1. 导入依赖 相应的依赖请参照
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.aliyun.api.gateway</groupId><artifactId>java.demo</artifactId><version>1.0-SNAPSHOT</version><dependencies><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></dependencies>
</project>
  1. 根据提供复制其 HttpUtils HttpUtils
package com.atguigu.gulimall.thirdparty.util;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;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);}}
}
  1. 测试代码:
    @Testpublic void sendSms() {String host = "https://dfsns.market.alicloudapi.com";String path = "/data/send_sms";String method = "POST";String appcode = "你的appcode";Map<String, String> headers = new HashMap<String, String>();//最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105headers.put("Authorization", "APPCODE " + appcode);//根据API的要求,定义相对应的Content-Typeheaders.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");Map<String, String> querys = new HashMap<String, String>();Map<String, String> bodys = new HashMap<String, String>();bodys.put("content", "code:hgw689,expire_at:2");bodys.put("phone_number", "187*****113");bodys.put("template_id", "TPL_0001");try {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();}}

AppCode 在工作台已购买的服务中看:

测试成功:

阿里云短信验证码购教程(Java演示)相关推荐

  1. 阿里云短信验证码服务使用(java ssm为例)

    注册并登陆阿里云账号 不知道到网址的点击此处 添加模板 填写签名,选择适用场景为验证码 ps:签名为验证码签名的标签,[阿里云]验证号码为000000,那个阿里云就是签名(适用场景为通用需要填写企业信 ...

  2. 【阿里云短信验证码】麻瓜教程~~~从注册---申请---代码---执行

    阿里云短信验证码 当然啦,学习任何东西第一步就是去注册当前网站的账号. 阿里云的官网:https://www.aliyun.com/?utm_content=se_1008364713 ◆[1.先注册 ...

  3. 使用阿里云短信验证码API发送短信验证码(配置,获取短信验证码,注册,登录,密码重置)

    获取阿里云短信验证码需要的配置信息. 如果是新用户,可以免费领取3个月,老用户的话就只能购买了,但是也不贵. 申请短信签名 申请短信模板 编写发送短信验证码的工具类 代码中我已经进行了详细的注释,也写 ...

  4. 阿里云短信验证码实战

    一.创建阿里云短信权限用户 1.登陆阿里云之后我们点击头像,接着点击AccessKey: 2.选择开始使用子用户 : 3.我们先要创建一个用户组: 4.依次点击新建的用户组--授权管理,给用户组授权, ...

  5. nodejs实现阿里云短信验证码

    nodejs实现阿里云短信验证码 事先准备 1.开通阿里云短信服务 2.获取 AccessKey 代码编写 事先准备 1.开通阿里云短信服务 1⃣️登陆阿里云,然后进入到 https://dysms. ...

  6. springboot 使用shiro集成阿里云短信验证码

    目录 1.阿里云短信验证码服务 2.发送短信验证码 3.shiro配置多个realm 4.验证短信验证码 5.一些修改思路 引言:短信验证码是通过发送验证码到手机的一种有效的验证码系统,主要用于验证用 ...

  7. C# ASP.NET MVC 阿里云短信验证码Demo

    相信大家在开发过程中又很多使用到验证码验证的功能,今天将验证码的验证整理一下写了一个Demo 通过本篇后你能学习到: 阿里云短信服务 Drapper连接SQL Server进行增改操作 JS前端倒计时 ...

  8. [风一样的创作]二次封装阿里云短信 验证码 发送短信 查询短信 编辑短信

    1.首先要注册阿里云,购买阿里云短信服务,拿到AccessKey ID和AccessKey Secret 链接: https://usercenter.console.aliyun.com/#/man ...

  9. 短信验证--阿里云短信验证码接口

    前言 公司最近项目需要一个手机验证码的功能,任务确定后,倍感亚历山大,以为和第三方对接的都好麻烦,查阿里的API.网上大神写的博客,各种查之后才发现,简单的一塌糊涂,这里想说个问题,不知道其他的攻城狮 ...

  10. 阿里云短信验证码(发送短信验证码)

    注意:在需在阿里云短信服务处申请accessKeyId,accessKeySecret,还有短信名头,短信模板填入下方空处 首先创建一个随机生成二维码的工具类CodeUtils public clas ...

最新文章

  1. [20180806]tune2fs调整保留块百分比.txt
  2. 厉害了!Python+matplotlib制作8个排序算法的动画
  3. 上传图片即时显示图片
  4. 可以使用中文作为变量名_次氯酸可以作为伤口消毒使用吗?
  5. Theano3.7-练习之堆叠消噪自动编码器
  6. js中document.documentElement 和document.body 以及其属性 clientWidth等
  7. jenkins访问地址_运维机器人hubot集成jenkins
  8. 菜刀php教程,Weevely(php菜刀)工具使用详解
  9. 互联网+正在颠覆行车记录仪市场
  10. ISO27145协议解析
  11. C# 软件开发岗面试经验总结
  12. 参与流片是一种怎样的体验?
  13. 最长公共子序列的问题
  14. android alert
  15. gnuplot用C语言程序画图,gnuplot使用
  16. RuntimeError: Could not find GCC executable.
  17. JavaWeb开发 —— HTML
  18. 【工具使用】Fireworks基本使用
  19. 平凡的世界和你我(田润生与郝红梅)
  20. Python入门到精通

热门文章

  1. 智能控制基础(6):自动控制原理第五版第二章答案(部分)
  2. Zigbee协议栈中文说明
  3. windows开机启动方法
  4. java 中怎么打印一个日历_日历打印用java实现
  5. 企业微信支付提示请在微信客户端打开链接_微信h5支付?
  6. Python数据处理Tips数据预处理操作方法汇总
  7. 使用IDEA搭建SSM项目
  8. 免费易用的Web版OFD阅读器
  9. NS和DNS的区别有哪些?
  10. 华为防火墙easy-ip配置