java写的后台调用云通信平台短信发送的代码:
云通信平台api地址:http://docs.cloopen.com/index.php/%E6%A8%A1%E6%9D%BF%E7%9F%AD%E4%BF%A1
package com.msg.util;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.aspectj.weaver.ast.Test;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
public class SmsUtils {
public static String LOGIN_URL;
public static String Authorization;
// http客户端
public static DefaultHttpClient httpclient;
public static HttpPost getPostMethod(String url){
HttpPost pmethod = new HttpPost(url); // 设置响应头信息
pmethod.addHeader("Accept", "application/json");
// Content-Type application/x-www-form-urlencoded; charset=UTF-8
pmethod.addHeader("Content-Type", "application/json; charset=UTF-8");
// Host mp.weixin.qq.com
pmethod.addHeader("Host", "app.cloopen.com:8883");
// X-Requested-With XMLHttpRequest
pmethod.addHeader("Authorization", Authorization);
return pmethod;
}
static {
httpclient = new DefaultHttpClient();
httpclient = (DefaultHttpClient) HttpClientConnectionManager.getSSLInstance(httpclient); // 接受任何证书的浏览器客户端
MD51 md5 = new MD51();
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
String datestr = format.format(date);
String sig = md5.getMD5ofStr(
"{accountSid}{appid}"
+ datestr).toUpperCase();
LOGIN_URL = "https://app.cloopen.com:8883/2013-12-26/Accounts/{accountSid}/SMS/TemplateSMS?sig="
+ sig;
Authorization=encodeStr("{accountSid}:"+datestr);
}
/**
*
* 创建日期2011-4-25上午10:12:38
* 修改日期
* 作者:dh *TODO 使用Base64加密算法加密字符串
*return
*/
public static String encodeStr(String plainText){
byte[] b=plainText.getBytes();
Base64 base64=new Base64();
b=base64.encode(b);
String s=new String(b);
return s;
}
/**
*
* 创建日期2011-4-25上午10:15:11
* 修改日期
* 作者:dh *TODO 使用Base64加密
*return
*/
public static String decodeStr(String encodeStr){
byte[] b=encodeStr.getBytes();
Base64 base64=new Base64();
b=base64.decode(b);
String s=new String(b);
return s;
}
/**
* 发送非模板短信
* @param args
* @author: Jerri Liu
* @date: 2014年3月19日下午2:28:42
*/
public static boolean sendMsg(Object phoneNumber,Object captcha){
HttpPost httpost = getPostMethod(LOGIN_URL);
String s = "{\"to\":\""+phoneNumber+"\",\"body\":\""+captcha+"\",\"msgType\":\"0\",\"appId\":\"{appid}\",\"subAccountSid\":\"{subAccountSid}\"}";
httpost.setEntity(new StringEntity(s, "UTF-8"));
try {
httpclient.execute(httpost);
return true;
} catch (Exception e) {
return false;
}
}
/**
* 发送模板短信
* @param phoneNumber 电话号
* @author: Jerri Liu
* @date: 2014年3月19日下午2:28:42
*/
public static String sendTemplateMsg(Object phoneNumber,Object captcha){
try {
HttpPost httpost = getPostMethod(LOGIN_URL);
String s = "{\"to\":\""+phoneNumber+"\"appId\":\"{appid}\",\"templateId\":\"1\",\"datas\":[\""+captcha+"\",\"3\"]}";
httpost.setEntity(new StringEntity(s, "UTF-8"));
HttpResponse response = httpclient.execute(httpost);
String jsonStr = EntityUtils.toString(response.getEntity(), "utf-8");
System.out.println(jsonStr);
JSONObject object = JSON.parseObject(jsonStr);
return object.getString("errmsg");
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
/**
* 云通信短信平台
* @param args
* @author: Jerri Liu
* @date: 2014年3月19日下午2:29:20
*/
public static void main(String[] args) {
String msg = testSendMsg("18754299621", RandomUtil.createRandomNum(6));
System.out.println(msg);
}
/**
* 鹏讯通短信平台
* @param args
* @author: Jerri Liu
* @date: 2014年3月19日下午2:28:42
*/
// public static void main(String[] args) {
// try {
// String result = sendSms("nihao wohao dajiahao", "18754299621");
// System.out.println(result);
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
/**
* @param nr 短信内容
* @param hm 全部被叫号码
* @return
*/
public static String sendSms(String nr, String hm) {
String result = null;
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.sms8810086.com/jk.aspx");
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("zh", "ceshi01")); //用户名称
params.add(new BasicNameValuePair("mm", "123")); //密码
params.add(new BasicNameValuePair("sms_type", "40")); //用户通道
params.add(new BasicNameValuePair("hm", hm)); //接受人电话号码,如果有多个以逗号隔开
params.add(new BasicNameValuePair("nr", nr)); //短信内容
try {
httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = null;
try {
instream = entity.getContent();
result = IOUtils.toString(instream, "utf-8");
} finally {
if (instream != null)
instream.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}java 调用云通信的api发送短信,布布扣,bubuko.com
原文:http://blog.csdn.net/ljasdf123/article/details/21541387