Java calls the cloud communication api to send text messages

Source: Internet
Author: User

The following code calls the cloud communication platform text message sending in the background written in java:

Cloud communication platform api address: 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 client public static DefaultHttpClient httpclient; public static HttpPost getPostMethod (String url) {HttpPost pmethod = new HttpPost (url); // you can specify the Response Header pmethod. addHeader ("Accept", "application/json"); // Content-Type application/x-www-f Orm-urlencoded; charset = UTF-8pmethod.addHeader ("Content-Type", "application/json; charset = UTF-8"); // Host mp. weixin. qq. compmethod. addHeader ("Host", "app.cloopen.com: 8883"); // X-Requested-With XMLHttpRequestpmethod. addHeader ("Authorization", Authorization); return pmethod;} static {httpclient = new DefaultHttpClient (); httpclient = (DefaultHttpClient) HttpClientConnectionManager. getSSLInstance (httpcl Ient); // browser client that accepts any certificate 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);}/*** creation date: 10:12:38 * modification date * Author: dh * TODO uses the Base64 encryption algorithm to encrypt the String * 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 ;} /***** creation date: 10:15:11 * modification date * Author: dh * TODO uses Base64 encryption * 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 ;} /*** send a non-template text message * @ param args * @ author: Jerri Liu * @ date: 2:28:42, January 1, March 19, 2014 */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 restarting httpclient.exe cute (httpost); return true ;}catch (Exception e) {return false ;}} /*** send template text message * @ param phoneNumber phone number * @ author: Jerri Liu * @ date: 2:28:42, January 1, March 19, 2014 */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.exe cute (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 "" ;}}/*** cloud communication SMS platform * @ param args * @ author: Jerri Liu * @ date: march 19, 2014 2:29:20 */public static void main (String [] args) {String msg = testSendMsg ("18754299621", RandomUtil. createRandomNum (6); System. out. println (msg);}/*** Peng xuntong SMS platform * @ param args * @ author: Jerri Liu * @ date: march 19, 2014 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 text message content * @ param hm all called numbers * @ 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 BasicNameValuePair ("zh", "ceshi01"); // user name params. add (new BasicNameValuePair ("mm", "123"); // password params. add (new BasicNameValuePair ("sms_type", "40"); // user channel params. add (new BasicNameValuePair ("hm", hm); // Number of the recipient's phone number. If multiple phone numbers are separated by commas. add (new BasicNameValuePair ("nr", nr); // try {httppost. setEntity (new UrlEncodedFormEntity (params, "UTF-8"); HttpResponse response = httpclien T.exe cute (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 ;}}
  
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.