Java常用工具类-发短信(集成河南华夏通信短信网关)

集成河南华夏通信短信网关,实现发短信功能,具体代码如下:

package com.zrsc.sendsms;


import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;


/**
 * 短信接口
 * 
 * 
 */
public class ShenZhouMsg {

    private static String NOTESTARTSIGN="【***】";//短信前的签名
    private static String SENDURL="http://203.171.239.15:8501/sendSmsadv.asp";
    private static String USERNAME="***";//发短信的账号
    private static String PASSWORD="***";//发短信密码对应的MD5值
    private static String VERIFICATION_CODE=NOTESTARTSIGN+"您的验证码是:1234";//短信验证码例子
    /**
     * @param args
     * @throws UnsupportedEncodingException 
     */
    public static void main(String[] args) throws UnsupportedEncodingException {
        // TODO Auto-generated method stub
        System.out.println(shenZhouPostMsg(SENDURL,USERNAME,PASSWORD,VERIFICATION_CODE,"18310646106"));
    }
    public static String shenZhouPostMsg(String url, String name, String pwd,
            String content, String mobile) throws UnsupportedEncodingException {

        StringBuilder strParamater = new StringBuilder();
        strParamater.append("username=");
        strParamater.append(name);
        strParamater.append("&password=");
        strParamater.append(pwd);
        strParamater.append("&destaddr=");
        strParamater.append(mobile);
        strParamater.append("&content=");
        strParamater.append(content);

        return postNote(url,
                strParamater.toString().getBytes("gb2312"), "gb2312");
    }

    
    private static String postNote(final String url, final byte[] data,
            String contentEncoding) {
        String result = null;
        int bufferSize = 1024;        
        HttpURLConnection httpURLConnection = null;
        InputStream inputStream = null;
        OutputStream outputStream = null;
        ByteArrayOutputStream byteArrayOutputStream = null;
        // String contentEncoding;
        byte[] buffer = new byte[bufferSize];
        int readBytes = 0;
        try {
            httpURLConnection = (HttpURLConnection) (new URL(url)
                    .openConnection());
            httpURLConnection.setConnectTimeout(30000);
            httpURLConnection.setDoInput(true);
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setUseCaches(false);
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setRequestProperty("Content-Type",
                    "application/x-www-form-urlencoded");
            httpURLConnection.setRequestProperty("Charset", "gb2312");

            // add post data
            outputStream = new DataOutputStream(
                    httpURLConnection.getOutputStream());
            outputStream.write(data);
            outputStream.flush();
            // get response
            // getCookie(httpURLConnection);
            byteArrayOutputStream = new ByteArrayOutputStream();
            if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                inputStream = new BufferedInputStream(
                        httpURLConnection.getInputStream());
                while ((readBytes = inputStream.read(buffer)) > 0) {
                    byteArrayOutputStream.write(buffer, 0, readBytes);
                }
            }
            result = new String(byteArrayOutputStream.toByteArray(),
                    contentEncoding);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }

            if (byteArrayOutputStream != null) {
                try {
                    byteArrayOutputStream.close();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            if (httpURLConnection != null) {
                try {
                    httpURLConnection.disconnect();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
        return result;
    }
}
 

短信发送成功后,会返回如下信息:

<meta http-equiv='Content-Type' content='text/html;charset=GB2312'> <html><body>你的信息已经成功提交1条/60001/20190319141720910-335</body></html> 

下载源码

源码包含如下

1.集成华软通信短信网关,已实现发验证码短信,对应Java文件为SMS106.java,没有依赖的jar。
2.集成河南华夏通信短信网关,已实现发验证码短信,对应Java文件为ShenZhouMsg.javar,没有依赖的jar。
3.集成云通信的企业信使短信平台,已实现发验证码短信,对应Java文件为MessageSenderUtil.javar,依赖的jar有httpclient-4.3.3.jar,httpcore-4.3.2.jar,commons-logging-1.1.1.jar,dom4j-1.6.1.jar。
4.集成阿里云短信服务,已实现发验证码短信,对应Java文件为AliSMS.java,依赖的jar有aliyun-java-sdk-core-4.1.0.jar,activation-1.1.1.jar,gson-2.8.5.jar,jaxb-api-2.1.jar,jaxb-core-2.3.0.1.jar,jaxb-impl-2.3.2.jar,httpclient-4.3.3.jar,httpcore-4.3.2.jar。
阿里云短信服务有对接文档,即《阿里短信集成详解.docx》。

综上所述,费用最便宜是集成阿里云短信服务,但技术对接最麻烦。如果想找个最简单对接又便宜的,建议使用河南华夏通信短信网关。

猜你喜欢

转载自blog.csdn.net/jlq_diligence/article/details/88661377