Java常用工具类-发短信(集成云通信的企业信使短信平台)

1、网站后台地址 http://sms.58yhkj.com/

2、java调用工具类


package com.chinautil.sendsms;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.util.EntityUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

/**
 * 短信发送工具类
 * 
 */
public class MessageSenderUtil {
    public static final String MSG_MOBILE_ADMIN="18310646106";
    private static String NOTESTARTSIGN="【***】";//短信前的签名
    private static String SENDURL="http://115.28.50.135:8888/sms.aspx";
    private static String USERID="***";//发短信的账号ID
    private static String USERNAME="***";//发短信的账号
    private static String PASSWORD="***";//发短信密码对应的MD5值
    private static String VERIFICATION_CODE=NOTESTARTSIGN+"您的验证码是:1234";//短信验证码例子
    /**
     * 
     * 功能描述:发送短信
     *
     */
    @SuppressWarnings("deprecation")
    public static boolean sendMsg(String mobile,String content){
        HttpPost httpRequest = null;
        try {
            HttpClient client = new org.apache.http.impl.client.DefaultHttpClient(
                    new PoolingClientConnectionManager());
            client.getParams().setIntParameter(
                    CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);
            client.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT,
                    5000);
            httpRequest = new HttpPost(SENDURL);
            //httpRequest.setURI(new URI(baseUrl));
            List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();  
            params.add(new BasicNameValuePair("action", "send"));  
            params.add(new BasicNameValuePair("userid", USERID)); 
            params.add(new BasicNameValuePair("account", USERNAME));
            params.add(new BasicNameValuePair("password", PASSWORD));
            params.add(new BasicNameValuePair("mobile", mobile));
            params.add(new BasicNameValuePair("content", content));
            UrlEncodedFormEntity entity =new UrlEncodedFormEntity(params, "UTF-8");  
            httpRequest.setEntity(entity);
            HttpResponse httpResponse = client.execute(httpRequest);
            if (null != httpResponse) {
                String result = EntityUtils.toString(httpResponse.getEntity());
                System.out.println(result);
                if("ok".equals(getSendMsgResult(result))){//0:代表成功
                    return true;
                }
            }
        } catch (Exception e) {
             throw new RuntimeException(e);
        } finally {
            if (httpRequest != null) {
                httpRequest.reset();
            }
        }
        return false;
    }

    private static String getSendMsgResult(String resultStr) throws DocumentException{
        Document document = DocumentHelper.parseText(resultStr);
        Element root = document.getRootElement();  
        Element message = root.element("message"); 
        return message.getText();
    }
    public static void main(String[] args) throws Exception {
        System.out.println(sendMsg(MSG_MOBILE_ADMIN,VERIFICATION_CODE));
    }
}
 
posted @ 2019-09-24 14:43  ourlang  阅读(736)  评论(0编辑  收藏  举报