包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;
}
}