java 调用云通信的api发送短信(二)
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
params = new ArrayList
(); params.add(new BasicNameva luePair("zh", "ceshi01")); //用户名称 params.add(new BasicNameva luePair("mm", "123")); //密码 params.add(new BasicNameva luePair("sms_type", "40")); //用户通道 params.add(new BasicNameva luePair("hm", hm)); //接受人电话号码,如果有多个以逗号隔开 params.add(new BasicNameva luePair("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; } }