文章目录

【笔记于学习尚硅谷课程所作】

1、购买接口:在阿里云购买免费体验的短信接口

2.查看接口使用方法

3.测试

4.后端测试

(1)加入工具类HttpUtils

package com.hanhan.gulimall.thirdparty.utils;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;import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
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;
/*** @author LFuser* @create 2020-06-02-11:18*/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);}}
}

(2)编写方法

package com.hanhan.gulimall.thirdparty.component;import com.hanhan.gulimall.thirdparty.utils.HttpUtils;
import lombok.Data;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;import java.util.HashMap;
import java.util.Map;/*** @author LFuser* @create 2020-06-02-11:23*/
@ConfigurationProperties(prefix = "spring.cloud.alicloud.sms")
@Data
@Component
public class SmsComponent {private String host;private String path;private String appcode;private String skin;private String sign;public void sendCode(String phone, String code) {String method = "GET";Map<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("code", code);querys.put("phone", phone);querys.put("skin", skin);querys.put("sign", sign);//JDK 1.8示例代码请在这里下载:  http://code.fegine.com/Tools.ziptry {HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);//System.out.println(response.toString());如不输出json, 请打开这行代码,打印调试头部状态码。//状态码: 200 正常;400 URL无效;401 appCode错误; 403 次数用完; 500 API网管错误//获取response的bodySystem.out.println(EntityUtils.toString(response.getEntity()));} catch (Exception e) {e.printStackTrace();}}
}

(3)在application.yml中配置

      sms:host: https://smsmsgs.market.alicloudapi.compath: /sms/skin: 5sign: 1appcode: ...(自己的code)

(4)测试

    @AutowiredSmsComponent smsComponent;@Testpublic void sendCode1(){smsComponent.sendCode("15200001111","666999");}

第三方服务--短信接口相关推荐

  1. 易语言对接第三方验证码短信接口demo

    本文为您提供了易语言版本的验证码短信接口对接DEMO示例 //接口类型:互亿无线触发短信接口,支持发送验证码短信.订单通知短信等. //账户注册:请通过该地址开通账户 http://user.ihuy ...

  2. 如何用ASP语言对接第三方验证码短信接口?

    ASP对接验证码短信接口DEMO示例 <%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%> <% '接口类型:互亿无 ...

  3. 使用第三方平台短信接口实现发送验证码

      还是第三方的小平台的demo简单,基本没有任何封装的东西,简单易懂好实现,基本就是填上账号就能用. package com.test;import java.io.BufferedReader; ...

  4. 会员注册短信接口被攻击,如何防刷?

    网站或者APP注册页面,因为获取短信验证码的功能是暴露在外的,短信接口有可能被短信轰炸机攻击,那短信验证码防刷策略如何做呢?首先我们来了解一下短信接口攻击: 什么是短信接口攻击? 个别用户出于不正当目 ...

  5. 阿里大鱼短信接口教程php,ECSHOP短信接口【ECSHOP阿里大鱼短信】ECSHOP短信插件手机短信服务设置教程-ECSHOP教程网...

    各位朋友大家好,感谢大家对ECSHOP教程网的关注与支持!今天为大家详细解说一下ECSHOP注册短信接口[ECSHOP阿里大鱼短信插件]ECSHOP手机短信服务设置教程: 1.首先登陆:http:// ...

  6. Phpyun系统vip第三方短信接口插件适配方法适合v6.1至v6.2

    php云人才系统刚刚发布了v6.2更新内容较多,功能也非常震撼,具体内容请参照官方把,但是 但是 但是 盼望已久的第三方短信接口没有加无法使用像阿里云这样的地方放短信接口,仍然只能使用他们自己平台的短 ...

  7. 短信接口在日常服务类行业的应用

    演艺票务系统,一键定制发送/秒收票券数据/确保信息安全无忧 水电气服务系统,定制短信,方便快捷,及时提醒,让民众放心的服务应用 旅游行业应用系统,旅游路线信息通知,行程通知,注意事项提醒,会员活动出行 ...

  8. php 调用移动第三方短信接口

    <?php header("Content-type:text/html; charset=UTF-8"); /*** Class SendApi*/ class SendA ...

  9. php-调用阿里云第三方短信接口

    去云市场找一个短信接口 https://market.aliyun.com/products/56928004/cmapi023305.html?spm=5176.2020520132.101.7.7 ...

  10. 我们公司的短信接口被刷了,瞬间损失两万,怎么解决?(短信接口被盗刷系列1)

    1 我们公司的短信接口被刷了,瞬间损失两万 前两天的中午像往常一样热,太阳不知疲倦的在天空燃烧,热跑了云彩和鸟儿,马上就要点燃空气和我的脑神经.为我和电脑降温的,是我简陋的书桌上的小电扇,没有它的话, ...

最新文章

  1. centos7 更新源 安装ifconfig
  2. 某阿里程序员求助:绩效背1,老板让他主动走!敢要n+1就在背调时说坏话!怎么办?网友:大不了鱼死网破!...
  3. 【大数据】如何用形象的比喻描述大数据的技术生态?Hadoop、Hive、Spark 之间是什么关系?
  4. python max函数_使用'key'和lambda表达式的python max函数
  5. 2.1 CPU 上下文切换(上)
  6. 民间借贷利息多少才合法?
  7. 2018/03/25
  8. 论文浅尝 | 使用变分推理做KBQA
  9. 接口自动化测试_Python自动化测试学习路线之接口自动化测试「模块四」
  10. 拷贝目录: 将D:\course拷贝到C盘根下.... 需要使用到: FileInputStream FileOutputStream
  11. 推荐一个比吴恩达的还要优质的机器学习课程
  12. 一个搜索迷宫出路的程序
  13. org.tigris.subversion.javahl.ClientException: Attempted to lock an already-locked dir异常解决方法...
  14. 计算机系统访问控制的功能,访问控制是为了限制访问主体对访问客体的访问权限,从而使计算机系统在合法范围内使用的安全措施,以下关于访问控制的叙述中,()是不正确的 - 信管网...
  15. 08.存储Cinder→5.场景学习→02.Create Volume→1.cinder-api处理过程
  16. 面向对象编程实例——句柄类的使用
  17. JAVA毕业设计高校教学资源共享平台计算机源码+lw文档+系统+调试部署+数据库
  18. 估计、偏差 、方差
  19. struts1和2的区别总结
  20. Mas短信开发增值服务平台建设

热门文章

  1. centos7 安装btsync
  2. 简单的音频转文字的转换方法
  3. 云开发地铁路线图小程序源码和配置教程
  4. 【引用】我国一、二级学科目录
  5. matlab中arma,ARMA模型构建及MATLAB实现.pdf
  6. 从阿里投资B站看动漫IP,二次元市场蕴含了怎样的价值?
  7. Verilog 级联IIR滤波器设计
  8. Segmentree beats!---吉如一线段树学习笔记
  9. ssoj 2279 磁力阵
  10. HDU2030-汉字机内码