码迷,mamicode.com
首页 > 编程语言 > 详细

java发送短信

时间:2015-02-28 11:30:49      阅读:160      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!