import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
public class AlibabaCloudSMS {
public static void main(String[] args) {
String accessKeyId = "your_access_key_id";
String accessKeySecret = "your_access_key_secret";
String phoneNumbers = "your_phone_number";
String signName = "your_sign_name";
String templateCode = "your_template_code";
String templateParam = "{"code":"123456"}";
try {
String url = "http://dysmsapi.aliyuncs.com/?Action=SendSms"
+ "&PhoneNumbers=" + URLEncoder.encode(phoneNumbers, "UTF-8")
+ "&SignName=" + URLEncoder.encode(signName, "UTF-8")
+ "&TemplateCode=" + URLEncoder.encode(templateCode, "UTF-8")
+ "&TemplateParam=" + URLEncoder.encode(templateParam, "UTF-8")
+ "&AccessKeyId=" + URLEncoder.encode(accessKeyId, "UTF-8")
+ "&SignatureMethod=HMAC-SHA1"
+ "&SignatureNonce=45e25e9b-0a6f-4e51-9c3a-731b488ec5fc"
+ "&SignatureVersion=1.0"
+ "&Timestamp=2018-12-05T23:56:34Z"
+ "&Signature=Signature";
System.out.println("SMS URL: " + url);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
import java.io.*;
import java.net.*;
import java.util.*;
public class SMSSender {
public static void main(String[] args) {
String url = "http://api.k780.com:88";
String appKey = "your_app_key";
Map<String, String> params = new HashMap<>();
params.put("app", "sms.send");
params.put("tempid", "your_template_id");
params.put("sign", "your_sign");
params.put("appkey", appKey);
params.put("mobile", "your_mobile_number");
params.put("content", "your_message_content");
try {
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(getParamsString(params));
wr.flush();
wr.close();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
private static String getParamsString(Map<String, String> params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
for (Map.Entry<String, String> entry : params.entrySet()) {
result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
result.append("&");
}
String resultString = result.toString();
return resultString.length() > 0 ? resultString.substring(0, resultString.length() - 1) : resultString;
}
}
发布者:luotuoemo,转转请注明出处:https://www.jintuiyun.com/156548.html