引言

自从去年开始学习JavaWeb之后,项目中一直用的就是邮箱验证,用了将近一年多。从黑马旅游网、JavaWeb购物商城、到如今的SSM传智健康😹😹😹 不过,这让我这样的技术(强迫症) 男生很难受😬😬😬

就在前几天,谷粒学院项目又遇到了这样一个需求,我就开始忙着申请、开通这个业务,最后得知:个人用户无法使用短信服务,必须是公司或者上线的业务才可申请 ,这就让我们这些苦逼的大学生为难了。 之后我尝试过 ​​​​

后来某一天来自佛祖的运气,让我通过一个弹幕得知,可以在阿里云 云市场购买其他云的短信服务,😘😘😘 妙极了!

❤️❤️❤️下面介绍如何使用阿里云时长购买并使用短信服务的详细流程!❤️❤️❤️

使用步骤

1、进入云市场官网 https://market.aliyun.com/

如何利用阿里云市场购买并使用短信服务_.net

2、搜索对应的产品

如何利用阿里云市场购买并使用短信服务_.net_02

3、进行购买

如何利用阿里云市场购买并使用短信服务_.net_03

4、购买成功后可以进入控制台进行查看

如何利用阿里云市场购买并使用短信服务_.net_04

如何利用阿里云市场购买并使用短信服务_.net_05

如何利用阿里云市场购买并使用短信服务_apache_06

5、如何使用

查看购买下方的指南

如何利用阿里云市场购买并使用短信服务_java_07

如何利用阿里云市场购买并使用短信服务_阿里云_08

如何利用阿里云市场购买并使用短信服务_apache_09

6、查看短信模板

如何利用阿里云市场购买并使用短信服务_阿里云_10

现在我们 mobile,templateId,param(后序自己进行字符串拼接)都有了,就差smsId了

7、联系客服进行身份认证,申请smsId.

如何申请: ​​http://help.guoyangyun.com/Problem/Certification.html​

如何利用阿里云市场购买并使用短信服务_java_11

客服的服务态度绝对是五星的,回复的very quickly,也很有耐心.

如何利用阿里云市场购买并使用短信服务_apache_12

认证成功后,客服会发送自己一个smsId:xxxxxxxxx

8、使用代码开始发送短信

如何利用阿里云市场购买并使用短信服务_java_13

具体代码

下面贴出Java发送验证码的详细代码

  • 依赖 pom.xml
<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.12</version>
<scope>test</scope>
</dependency>
</dependencies>
  • httpUtils
package com.rg.sms;

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);
}
}
}
  • SMSUtils类
package com.rg.sms;

import org.apache.http.HttpResponse;
import java.util.HashMap;
import java.util.Map;

/**
* @author lxy
* @version 1.0
* @Description
* @date 2022/3/6 14:35
*/
public class SmsUtil {
/**
*
* @param mobile 手机号
* @param code 发送的验证码
* @param minute 多长时间有效
* @param smsSignId 签名id
* @param templateId 模板id
* @param minute
*/
public static void sendMessage(String appCode,String mobile,String code,String minute,String smsSignId,String templateId){
String host = "https://gyytz.market.alicloudapi.com";
String path = "/sms/smsSend";
String method = "POST";
Map<String, String> headers = new HashMap<String, String>();
//最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
headers.put("Authorization", "APPCODE " + appCode);
Map<String, String> querys = new HashMap<String, String>();
querys.put("mobile", mobile);
querys.put("param", "**code**:"+code+",**minute**:"+minute);
querys.put("smsSignId", smsSignId);
querys.put("templateId", templateId);
Map <String, String> bodys = new HashMap <String, String>();


try {
/**
* 重要提示如下:
* HttpUtils请从12
* https:///aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
* 下载
*
* 相应的依赖请参照
* https:///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();
}
}
}
  • Demo
/**
* @author lxy
* @version 1.0
* @Description
* @date 2022/3/6 15:19
*/
public class Demo {
public static void main(String[] args) {
SmsUtil.sendMessage("xxxxxx","xxxxxx","999999","30","xxxxxxxxxxxx","a09602b817fd47e59e7c6e603d3f088d");
}
}
  • 查看发送结果:

如何利用阿里云市场购买并使用短信服务_java_14

如何利用阿里云市场购买并使用短信服务_java_15