生活随笔
收集整理的這篇文章主要介紹了
网易云短信群发功能
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
步驟:
1.開通功能
2.創建簽名,簽名就是短信正文前的幾個字,通常是公司名稱的簡寫.
3.創建短信模版.
貼代碼:
package cloud.cloud
;
import java.security.MessageDigest
;
/**
* 這里面的內容不用修改
*/
class CheckSumBuilder {
// 計算并獲取CheckSum
public static String
getCheckSum(String appSecret
, String nonce
, String curTime) {
return encode(
"sha1", appSecret + nonce + curTime)
;
}
// 計算并獲取md5值
public static String
getMD5(String requestBody) {
return encode(
"md5", requestBody)
;
}
private static String
encode(String algorithm
, String value) {
if (value ==
null) {
return null;
}
try {MessageDigest messageDigest= MessageDigest.
getInstance(algorithm)
;
messageDigest.update(value.getBytes())
;
return getFormattedText(messageDigest.digest())
;
}
catch (Exception e) {
throw new RuntimeException(e)
;
}}
private static String
getFormattedText(
byte[] bytes) {
int len = bytes.
length;
StringBuilder buf =
new StringBuilder(len *
2)
;
for (
int j =
0; j < len
; j++) {buf.append(
HEX_DIGITS[(bytes[j] >>
4) &
0x0f])
;
buf.append(
HEX_DIGITS[bytes[j] &
0x0f])
;
}
return buf.toString()
;
}
private static final char[]
HEX_DIGITS = {
'0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }
;
}
package cloud.cloud
;
import com.alibaba.fastjson.JSON
;
import java.io.BufferedReader
;
import java.io.IOException
;
import java.io.InputStreamReader
;
import java.io.PrintWriter
;
import java.net.URL
;
import java.net.URLConnection
;
import java.net.URLEncoder
;
import java.util.Date
;
/**
* 發送模板短信請求,主要修改這里面的內容
* @author liuxuanlin
*
*/
public class SendCode
//注意自己的類名對應好!
{
public static void main(String[] args)
throws Exception{System.
out.println(
sendMsg())
;
}
/**
* 發送POST方法的請求
*
* @return 所代表遠程資源的響應結果
*/
public static String
sendMsg(){String appKey =
"7f35583355cae14d58db101ee4c64e6c";//后臺appKey管理中有
String appSecret =
"8623f195857c";//后臺appKey管理中有
String nonce =
"baoluo"; // 隨機數(最大長度128個字符),不用修改
String curTime = String.
valueOf((
new Date()).getTime() /
1000L)
; // 當前UTC時間戳,不用修改
System.
out.println(
"curTime: " + curTime)
;
String checkSum = CheckSumBuilder.
getCheckSum(appSecret
, nonce
, curTime)
;
System.
out.println(
"checkSum: " + checkSum)
;
PrintWriter out =
null;
BufferedReader in =
null;
String result =
"";
try
{String url =
"https://api.netease.im/sms/sendtemplate.action"; //網址可以不修改,不用修改
String encStr1 = URLEncoder.
encode(
"Tom", "utf-8")
;//短信模板中使用%s時,可以在程序中自定義變內容
String encStr2 = URLEncoder.
encode(
"name", "utf-8")
; // url編碼;防止不識別中文
//群發短信,需轉換String[]為json格式.需要用fastjson,tel是自建類,用于存放群發的短信stringp[]格式
String json_arr_String = JSON.
toJSONString(tel.
tels30)
;
String params =
"templateid=3902929&mobiles="+json_arr_String
//json_arr_String為json格式的目標短信,templateid是短信模版編號,后臺申請后獲得
+
"¶ms=" +
"[\"" + encStr1 +
"\",\""+ encStr2 +
"\"]"; //在params后添加各種變量參數
System.
out.println(
"params:" + params)
;
URL realUrl =
new URL(url)
;
// 打開和URL之間的連接
URLConnection conn = realUrl.openConnection()
;
// 設置通用的請求屬性
conn.setRequestProperty(
"AppKey", appKey)
;
conn.setRequestProperty(
"CheckSum", checkSum)
;
conn.setRequestProperty(
"CurTime", curTime)
;
conn.setRequestProperty(
"Content-Type", "application/x-www-form-urlencoded;charset=utf-8")
;
conn.setRequestProperty(
"Nonce", nonce)
;
// 發送POST請求必須設置如下兩行
conn.setDoOutput(
true)
;
conn.setDoInput(
true)
;
// 獲取URLConnection對象對應的輸出流
out =
new PrintWriter(conn.getOutputStream())
;
// 發送請求參數
out.print(params)
;
// flush輸出流的緩沖
out.flush()
;
// 定義BufferedReader輸入流來讀取URL的響應
System.
out.println(conn)
;
in =
new BufferedReader(
new InputStreamReader(conn.getInputStream()))
;
System.
out.println(
"in"+in)
;
String line
;
while ((line = in.readLine()) !=
null){result += line
;
}}
catch (Exception e){System.
out.println(
"發送 POST 請求出現異常!\n" + e)
;
e.printStackTrace()
;
}
// 使用finally塊來關閉輸出流、輸入流
finally
{
try
{
if (out !=
null){out.close()
;
}
if (in !=
null){in.close()
;
}}
catch (IOException ex){ex.printStackTrace()
;
}}
return result
;
}}
總結
以上是生活随笔為你收集整理的网易云短信群发功能的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。