자바 클 라 우 드 통신 api 호출 문자

자바 가 쓴 배경 에서 클 라 우 드 통신 플랫폼 문자 로 보 낸 코드 를 호출 합 니 다.
클 라 우 드 통신 플랫폼 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;  
    }  
}

좋은 웹페이지 즐겨찾기