Java调用阿里云API发送短信
购买发送短信服务地址,说明中有各语言调用示例、参数说明及测试用的签名id和模板id
https://market.aliyun.com//products/57126001/cmapi00037415.html?spm=5176.730005.result.8.59d33524Zr6MHR#sku=yuncode3141500001
其中模板为“验证码:**code**,**minute**分钟内有效,请勿泄露于他人!”
更换模板需要联系客服修改
需要的jar包
<dependency>
<groupId>com.alibaba.csb.sdk</groupId>
<artifactId>http-client</artifactId>
<version>1.1.5.1</version>
</dependency>
java代码
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class TestUtil {
public static void sendShortMessage() {
String host = "https://gyytz.market.alicloudapi.com/sms/smsSend";
String appcode = "自己的appcode";//购买产品后给的appcode
Map<String, String> headers = new HashMap<String, String>();
headers.put("Authorization", "APPCODE " + appcode);
Map<String, String> querys = new HashMap<String, String>();
querys.put("mobile", "手机号");//手机号码
querys.put("param", "**code**:147258,**minute**:5");//模板中的参数
querys.put("smsSignId", "2e65b1bb3d054466b82f0c9d125465e2");//签名id
querys.put("templateId", "908e94ccf08b4476ba6c876d13f084ad");//模板id
Map<String, String> bodys = new HashMap<String, String>();
try {
HttpClient httpClient = HttpClients.createDefault();
HttpPost request = new HttpPost(buildUrl(host, 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);
}
HttpResponse response = httpClient.execute(request);
System.out.println(response.toString());
int code = response.getStatusLine().getStatusCode();
System.out.println(code);
String data = EntityUtils.toString(response.getEntity(), "utf-8");
System.out.println(data);
} catch (Exception e) {
e.printStackTrace();
}
}
private static String buildUrl(String host, Map<String, String> querys) throws Exception{
StringBuilder sbUrl = new StringBuilder();
sbUrl.append(host);
if (null != querys) {
StringBuilder sbQuery = new StringBuilder();
for (Map.Entry<String, String> query : querys.entrySet()) {
if (0 < sbQuery.length()) {
sbQuery.append("&");
}
if (isBlank(query.getKey()) && !isBlank(query.getValue())) {
sbQuery.append(query.getValue());
}
if (!isBlank(query.getKey())) {
sbQuery.append(query.getKey());
if (!isBlank(query.getValue())) {
sbQuery.append("=");
sbQuery.append(URLEncoder.encode(query.getValue(), "utf-8"));
}
}
}
if (0 < sbQuery.length()) {
sbUrl.append("?").append(sbQuery);
}
}
return sbUrl.toString();
}
public static boolean isBlank(String str) {
int strLen;
if (str == null || (strLen = str.length()) == 0)
return true;
for (int i = 0; i < strLen; i++) {
if (!Character.isWhitespace(str.charAt(i)))
return false;
}
return true;
}
public static void main(String[] args) {
sendShortMessage();
}
}