阿里大于短信发送-Java版:
简介:
阿里大于短信服务可以说是非常方便的,实现这个功能,只能三步:一是注册阿里云账号,购买短信服务;二是购买成功后设置短信签名,短信模板,申请成功后进行在线调试; 三是复制调用代码到本地,测试ok,下面我们就来看看详细步骤:(如图片看不清,右键图片新窗口打开查看即可)
下面是短信签名和模板示例:
在线调试接口:
调试参数:
调试成功后,copy相应java块代码:
1 package main.com.wangyao.utils; 2 3 import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest; 4 import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse; 5 import org.apache.http.HttpResponse; 6 7 import java.util.HashMap; 8 import java.util.Map; 9 import java.util.Random; 10 11 public class SMSSender { 12 13 //发送短信 14 public static void main(String[] args) throws ApiException { 15 Random num=new Random(); 16 int a=num.nextInt(999999); 17 String tel = "13260xxxx"; 18 String host = "http://sms.market.alicloudapi.com"; 19 String path = "/singleSendSms"; 20 String method = "GET"; 21 String appcode = "xxxxx";//写上你自己的appcode 22 Map<String, String> headers = new HashMap<String, String>(); 23 //最后在header中的格式(中间是英文空格)为Authorization:APPCODE eba7428326af4daea82c738675b3669f 24 headers.put("Authorization", "APPCODE " + appcode); 25 Map<String, String> querys = new HashMap<String, String>(); 26 querys.put("ParamString", "{\"code\":\"123456\"}"); 27 querys.put("RecNum", tel); 28 querys.put("SignName", "王尧"); 29 querys.put("TemplateCode", "SMS_105105012"); 30 try { 31 HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys); 32 System.out.println(response.toString()); 33 System.out.println(response.getEntity().getContent().toString()); 34 } catch (Exception e) { 35 e.printStackTrace(); 36 } 37 } 38 }
完了。