首页 > 编程语言 > 详细

java发送短信

时间:2015-02-28 11:28:36      阅读:239      评论:0      收藏:0      [点我收藏+]
package util;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.SimpleHttpConnectionManager;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.log4j.Logger;
public class SendSmsUtil {
    String smsurl = "http://115.239.133.245:8080/YL_sms/http/sendSMS";

    /**
     * 发送短信
     * 
     * @param url
     * @param phone
     * @param content
     * @param netType
     * @param gwTyep
     * @return
     */
    public String sendSms(String phone, String content) {
        PostMethod postMethod = null;
        HttpClient httpClient = null;
        String returnValue = "";

        String netType = "1";
        String gwType = "8";
        if(isChinaTeleCom(phone)){
            netType="0";
            gwType="32";
        }

        try {
            postMethod = new PostMethod(smsurl);// 通知接口
            postMethod.getParams().setParameter(
                    HttpMethodParams.HTTP_CONTENT_CHARSET, "gbk");
            postMethod.setParameter("USERID", "file");
            postMethod.setParameter("PASSWORD", "123456");
            postMethod.setParameter("PHONENO", phone);
            postMethod.setParameter("SMSTEXT", content);
            postMethod.setParameter("NETTYPE", netType);
            postMethod.setParameter("ECPNO", phone);
            postMethod.setParameter("SENDERECP", "");
            postMethod.setParameter("SENDERNAME", "");
            postMethod.setParameter("PR", "5");
            postMethod.setParameter("FLAG", "1");
            postMethod.setParameter("GWTYPE", gwType);

            httpClient = new HttpClient();
            // 设置超时时间10秒
            httpClient.getHttpConnectionManager().getParams()
                    .setConnectionTimeout(10000);
            // 设置返回超时10秒
            httpClient.getHttpConnectionManager().getParams().setSoTimeout(
                    10000);
            int statusCode = httpClient.executeMethod(postMethod);
            if (statusCode != HttpStatus.SC_OK) {
                log.info("渠道处理 failed: " + postMethod.getStatusLine());
                returnValue = "fail";
            } else {
                log.info("调用渠道处理接口成功!");
                returnValue = "success";
            }
        } catch (Exception e) {
            log.error("发送短信异常:", e);
            returnValue = "fail";
        } finally {
            if (postMethod != null) {
                postMethod.releaseConnection();
                postMethod = null;
            }
            ((SimpleHttpConnectionManager) httpClient
                    .getHttpConnectionManager()).shutdown();
        }
        return returnValue;
    }

    /**
     * 判断是否电信号码
     * 
     * @param mobile
     * @return
     */
    public boolean isChinaTeleCom(String mobile) {
        boolean result = false;
        try {
            if (mobile.trim().startsWith("133")
                    || mobile.trim().startsWith("153")
                    || mobile.trim().startsWith("189")
                    || mobile.trim().startsWith("180")) {
                result = true;
            }
        } catch (Exception e) {
            result = false;
            log.error("判断是否电信号码异常:", e);
        }
        return result;
    }
}

java发送短信

原文:http://blog.csdn.net/u011328015/article/details/43982979

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!