java 片雲傳送簡訊
阿新 • • 發佈:2018-11-15
包sm;
/ **
*由bingone在15/12/16建立。
* /
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/ **
*簡訊http介面的java程式碼呼叫示例
*基於Apache HttpClient 4.3
*
* @作者songchao
* @since 2015-04-03
* /
public class JavaSmsApi2 {
// 檢視賬戶資訊的http地址
private static String URI_GET_USER_INFO =
“ https://sms.yunpian.com/v2/user/get.json“;
//智慧匹配模板發 ? 介面的http地址
private static String URI_SEND_SMS =
“https://sms.yunpian.com/v2/sms/single_send.json”;
//模板發 ?介面的http地址
private static String URI_TPL_SEND_SMS =
“https://sms.yunpian.com/v2/sms/tpl_single_send.json”;
//發?語音驗證碼介面的HTTP地址
私人靜態字串URI_SEND_VOICE =
“https://voice.yunpian.com/v2/voice/send.json”;
//繫結主叫,被叫關係的介面http地址
private static String URI_SEND_BIND =
“https://call.yunpian.com/v2/call/bind.json”;
//解綁主叫,被叫關係的介面http地址
private static String URI_SEND_UNBIND =
“https://call.yunpian.com/v2/call/unbind.json”;
//編碼格式。傳送編碼格式統一使用UTF-8
private static String ENCODING =“UTF-8”;
public static void main(String [] args)throws IOException,
URISyntaxException {
//修改為您的apikey.apikey可在官網(http://www.yunpian.com)登入後獲取?
String apikey =“13898519a92d32e6844f36269ecb1c7e”;
String mobile =“15002770045”;
/ ****************查賬戶資訊呼叫示?***************** /
System.out.println(JavaSmsApi2.getUserInfo(apikey));
/ ****************使用智慧匹配模板介面發簡訊?(推薦)***************** /
//設定您要發的內容(內容必須和某個模板匹配以下例子匹配的是系統提供的1號模板?)?
字串文字= “【雲片網】您的驗證碼1234?”;
/ /發簡訊呼叫示例
/// System.out.println(JavaSmsApi.sendSms(apikey,text,mobile));
/ ****************使用指定模板介面發短(不推薦,建議使用智慧匹配模板介面)****** /
//設定模板ID,如使用1號模板?:?#company#]您的驗證碼?#code#
long tpl_id = 2110580;
//設定對應的模板變數 ??
字串tpl_value2 = URLEncoder.encode(“#code#”,ENCODING)+“=”+
URLEncoder.encode(“1234”,ENCODING)+“&”+ URLEncoder.encode(
“#code#”,ENCODING)+“= “+ URLEncoder.encode(”雲片 ?“,
ENCODING);
字串tpl_value = URLEncoder.encode(“#code#”,ENCODING)+“=”+
URLEncoder.encode(“#code#”,ENCODING);
?//模板發的呼叫示例
的System.out.println(tpl_value);
System.out.println(JavaSmsApi2.tplSendSms(apikey,tpl_id,tpl_value,
mobile));
//System.out.println(JavaSmsApi.sendVoice(apikey,mobile,code));
/ ****************使用介面繫結主被叫號碼?***************** /
String from =“+ 86130xxxxxxxx”;
String to =“+ 86131xxxxxxxx”;
整數時間= 30 * 60; //繫結30分鐘
//System.out.println(JavaSmsApi.bindCall(apikey,from,to,duration));
/ ****************使用介面解綁主被叫號碼?***************** /
//System.out.println(JavaSmsApi.unbindCall(apikey,from,to));
}
/ **
*取賬戶信 ?
*
* @return json格式字元 ?
* @ throws java.io.IOException
* /
public static String getUserInfo(String apikey)throws IOException,
URISyntaxException {
Map <String,String> params = new HashMap <String,String>();
params.put(“apikey”,apikey);
返回帖子(URI_GET_USER_INFO,params);
}
/ **
*智慧匹配模板介面發短 ?
*
* @param apikey apikey
* @param text *?簡訊內容
* @param mobile??接受的手機號
* @return json格式字元?
*丟擲IOException
* /
public static String sendSms(String apikey,String text,
String mobile)throws IOException {
Map <String,String> params = new HashMap <String,String>();
params.put(“apikey”,apikey);
params.put(“text”,text);
params.put(“手機”,手機);
返回帖子(URI_SEND_SMS,params);
}
/ **
*通過模板傳送?(不推薦)?
*
* @param apikey apikey
* @param tpl_id?模板ID
* @引數tpl_value?模板變數?
* @param mobile ?接受的手機號
* @return json格式字元 ?
* @throws IOException
* /
public static String tplSendSms(String apikey,long tpl_id,String tpl_value,
String mobile)throws IOException {
Map <String,String> params = new HashMap <String,String>();
params.put(“apikey”,apikey);
params.put(“tpl_id”,String.valueOf(tpl_id));
params.put(“tpl_value”,tpl_value);
params.put(“手機”,手機);
返回帖子(URI_TPL_SEND_SMS,params);
}
/ **
*通過介面傳送?語音驗證碼
* @param apikey apikey
* @param mobile接收的手機號
* @param code驗證?
* @return
* /
public static String sendVoice(String apikey,String mobile,String code){
Map <String,String> params = new HashMap <String,String>();
params.put(”
params.put(“手機”,手機);
params.put(“code”,code);
返回帖子(URI_SEND_VOICE,params);
}
/ **
*通過介面繫結主被叫號碼?
* @param apikey apikey
* @param from主叫
* @param to called叫
* @param duration有效時長,單位: ?
* @return
* /
public static String bindCall(String apikey,String from,String to,
Integer duration){
Map <String,String> params = new HashMap <String,String>();
params.put(“apikey”,apikey);
params.put(“from”,from);
params.put(“to”,to);
params.put( “持續時間”,
返回帖子(URI_SEND_BIND,params);
}
/ **
*通過介面解綁繫結主被叫號碼?
* @param apikey apikey
* @param from主叫
* @param to被叫
* @return
* /
public static String unbindCall(String apikey,String from,String to){
Map <String,String> params = new HashMap <String, String>();
params.put(“apikey”,apikey);
params.put(“from”,from);
params.put(“to”,to);
返回帖子(URI_SEND_UNBIND,params);
}
/ **
*基於HttpClient 4.3的使用POST方法
*
* @param url提交的URL
* @param paramsMap提交<
* @return提交響應
* /
public static String post(String url,Map <String,String> paramsMap){
CloseableHttpClient client = HttpClients.createDefault();
String responseText =“”;
CloseableHttpResponse response = null;
嘗試{
HttpPost方法=新HttpPost(url);
if(paramsMap!= null){
List <NameValuePair> paramList = new ArrayList <
NameValuePair>();
for(Map.Entry <String,String> param:paramsMap.entrySet()){
NameValuePair pair = new BasicNameValuePair(param.getKey(),
param.getValue());
paramList.add(對);
}
method.setEntity(new UrlEncodedFormEntity(paramList,
ENCODING));
}
response = client.execute(method);
HttpEntity entity = response.getEntity();
if(entity!= null){
responseText = EntityUtils.toString(entity,ENCODING);
}
} catch(Exception e){
e.printStackTrace();
} finally {
try {
response.close();
catch(Exception e){
e.printStackTrace();
}
}
返回responseText;
}
}
/ **
*由bingone在15/12/16建立。
* /
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/ **
*簡訊http介面的java程式碼呼叫示例
*基於Apache HttpClient 4.3
*
* @作者songchao
* @since 2015-04-03
* /
public class JavaSmsApi2 {
// 檢視賬戶資訊的http地址
private static String URI_GET_USER_INFO =
“ https://sms.yunpian.com/v2/user/get.json“;
//智慧匹配模板發 ? 介面的http地址
private static String URI_SEND_SMS =
“https://sms.yunpian.com/v2/sms/single_send.json”;
//模板發 ?介面的http地址
private static String URI_TPL_SEND_SMS =
“https://sms.yunpian.com/v2/sms/tpl_single_send.json”;
//發?語音驗證碼介面的HTTP地址
私人靜態字串URI_SEND_VOICE =
“https://voice.yunpian.com/v2/voice/send.json”;
//繫結主叫,被叫關係的介面http地址
private static String URI_SEND_BIND =
“https://call.yunpian.com/v2/call/bind.json”;
//解綁主叫,被叫關係的介面http地址
private static String URI_SEND_UNBIND =
“https://call.yunpian.com/v2/call/unbind.json”;
//編碼格式。傳送編碼格式統一使用UTF-8
private static String ENCODING =“UTF-8”;
public static void main(String [] args)throws IOException,
URISyntaxException {
//修改為您的apikey.apikey可在官網(http://www.yunpian.com)登入後獲取?
String apikey =“13898519a92d32e6844f36269ecb1c7e”;
String mobile =“15002770045”;
/ ****************查賬戶資訊呼叫示?***************** /
System.out.println(JavaSmsApi2.getUserInfo(apikey));
/ ****************使用智慧匹配模板介面發簡訊?(推薦)***************** /
//設定您要發的內容(內容必須和某個模板匹配以下例子匹配的是系統提供的1號模板?)?
字串文字= “【雲片網】您的驗證碼1234?”;
/ /發簡訊呼叫示例
/// System.out.println(JavaSmsApi.sendSms(apikey,text,mobile));
/ ****************使用指定模板介面發短(不推薦,建議使用智慧匹配模板介面)****** /
//設定模板ID,如使用1號模板?:?#company#]您的驗證碼?#code#
long tpl_id = 2110580;
//設定對應的模板變數 ??
字串tpl_value2 = URLEncoder.encode(“#code#”,ENCODING)+“=”+
URLEncoder.encode(“1234”,ENCODING)+“&”+ URLEncoder.encode(
“#code#”,ENCODING)+“= “+ URLEncoder.encode(”雲片 ?“,
ENCODING);
字串tpl_value = URLEncoder.encode(“#code#”,ENCODING)+“=”+
URLEncoder.encode(“#code#”,ENCODING);
?//模板發的呼叫示例
的System.out.println(tpl_value);
System.out.println(JavaSmsApi2.tplSendSms(apikey,tpl_id,tpl_value,
mobile));
//System.out.println(JavaSmsApi.sendVoice(apikey,mobile,code));
/ ****************使用介面繫結主被叫號碼?***************** /
String from =“+ 86130xxxxxxxx”;
String to =“+ 86131xxxxxxxx”;
整數時間= 30 * 60; //繫結30分鐘
//System.out.println(JavaSmsApi.bindCall(apikey,from,to,duration));
/ ****************使用介面解綁主被叫號碼?***************** /
//System.out.println(JavaSmsApi.unbindCall(apikey,from,to));
}
/ **
*取賬戶信 ?
*
* @return json格式字元 ?
* @ throws java.io.IOException
* /
public static String getUserInfo(String apikey)throws IOException,
URISyntaxException {
Map <String,String> params = new HashMap <String,String>();
params.put(“apikey”,apikey);
返回帖子(URI_GET_USER_INFO,params);
}
/ **
*智慧匹配模板介面發短 ?
*
* @param apikey apikey
* @param text *?簡訊內容
* @param mobile??接受的手機號
* @return json格式字元?
*丟擲IOException
* /
public static String sendSms(String apikey,String text,
String mobile)throws IOException {
Map <String,String> params = new HashMap <String,String>();
params.put(“apikey”,apikey);
params.put(“text”,text);
params.put(“手機”,手機);
返回帖子(URI_SEND_SMS,params);
}
/ **
*通過模板傳送?(不推薦)?
*
* @param apikey apikey
* @param tpl_id?模板ID
* @引數tpl_value?模板變數?
* @param mobile ?接受的手機號
* @return json格式字元 ?
* @throws IOException
* /
public static String tplSendSms(String apikey,long tpl_id,String tpl_value,
String mobile)throws IOException {
Map <String,String> params = new HashMap <String,String>();
params.put(“apikey”,apikey);
params.put(“tpl_id”,String.valueOf(tpl_id));
params.put(“tpl_value”,tpl_value);
params.put(“手機”,手機);
返回帖子(URI_TPL_SEND_SMS,params);
}
/ **
*通過介面傳送?語音驗證碼
* @param apikey apikey
* @param mobile接收的手機號
* @param code驗證?
* @return
* /
public static String sendVoice(String apikey,String mobile,String code){
Map <String,String> params = new HashMap <String,String>();
params.put(”
params.put(“手機”,手機);
params.put(“code”,code);
返回帖子(URI_SEND_VOICE,params);
}
/ **
*通過介面繫結主被叫號碼?
* @param apikey apikey
* @param from主叫
* @param to called叫
* @param duration有效時長,單位: ?
* @return
* /
public static String bindCall(String apikey,String from,String to,
Integer duration){
Map <String,String> params = new HashMap <String,String>();
params.put(“apikey”,apikey);
params.put(“from”,from);
params.put(“to”,to);
params.put( “持續時間”,
返回帖子(URI_SEND_BIND,params);
}
/ **
*通過介面解綁繫結主被叫號碼?
* @param apikey apikey
* @param from主叫
* @param to被叫
* @return
* /
public static String unbindCall(String apikey,String from,String to){
Map <String,String> params = new HashMap <String, String>();
params.put(“apikey”,apikey);
params.put(“from”,from);
params.put(“to”,to);
返回帖子(URI_SEND_UNBIND,params);
}
/ **
*基於HttpClient 4.3的使用POST方法
*
* @param url提交的URL
* @param paramsMap提交<
* @return提交響應
* /
public static String post(String url,Map <String,String> paramsMap){
CloseableHttpClient client = HttpClients.createDefault();
String responseText =“”;
CloseableHttpResponse response = null;
嘗試{
HttpPost方法=新HttpPost(url);
if(paramsMap!= null){
List <NameValuePair> paramList = new ArrayList <
NameValuePair>();
for(Map.Entry <String,String> param:paramsMap.entrySet()){
NameValuePair pair = new BasicNameValuePair(param.getKey(),
param.getValue());
paramList.add(對);
}
method.setEntity(new UrlEncodedFormEntity(paramList,
ENCODING));
}
response = client.execute(method);
HttpEntity entity = response.getEntity();
if(entity!= null){
responseText = EntityUtils.toString(entity,ENCODING);
}
} catch(Exception e){
e.printStackTrace();
} finally {
try {
response.close();
catch(Exception e){
e.printStackTrace();
}
}
返回responseText;
}
}