用过的短信发送代码

一:

package cn.primeledger.bank.service.core.mc.service;

import cn.primeledger.bank.service.core.mc.constant.SMSResponseCode;
import cn.primeledger.bank.service.core.mc.exception.SMSException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 * 短信发送接口
 */
@PropertySource(value = "sms.properties", encoding = "utf-8")
@Service
public class SMSSenderService {

    private Logger LOGGER = LoggerFactory.getLogger(SMSSenderService.class);

    //请求url
    @Value("${sms.url}")
    private String url;
    //机构用户名,短信提供商提供
    @Value("${sms.username}")
    private String username;
    //机构密码,短信提供商提供
    @Value("${sms.password}")
    private String password;
    //短信签名
    @Value("${sms.sige}")
    private String sign;

    /**
     * 发送单条短信
     *
     * @param mobileNumber 手机号码
     * @param content      短信内容
     */
    public void send(String mobileNumber, String content) {
        LOGGER.info("start into sms send. accept=[{}]", mobileNumber);
        // 创建默认的httpClient实例.
        CloseableHttpClient httpclient = HttpClients.createDefault();
        // 创建httppost
        HttpPost httppost = new HttpPost(url);
        // 创建参数队列
        List<BasicNameValuePair> formparams = new ArrayList<BasicNameValuePair>();
        formparams.add(new BasicNameValuePair("username", username));
        formparams.add(new BasicNameValuePair("secret_key", password));
        formparams.add(new BasicNameValuePair("to", mobileNumber));
        formparams.add(new BasicNameValuePair("body", sign + content));
        UrlEncodedFormEntity uefEntity;
        try {
            uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
            httppost.setEntity(uefEntity);
            CloseableHttpResponse response = httpclient.execute(httppost);
            try {
                int status = response.getStatusLine().getStatusCode();
                LOGGER.info("end send mc. status=[{}]", status);
                if (status != 200) {
                    throw new SMSException(SMSResponseCode.getMsgByCode(status).orElse("短信未知错误,status=" + status));
                }
            } finally {
                response.close();
            }
        } catch (ClientProtocolException e) {
            LOGGER.error("send sms ClientProtocolException", e);
            throw new SMSException("send sms ClientProtocolException", e);
        } catch (UnsupportedEncodingException e) {
            LOGGER.error("send sms UnsupportedEncodingException", e);
            throw new SMSException("send sms UnsupportedEncodingException", e);
        } catch (IOException e) {
            LOGGER.error("send sms IOException", e);
            throw new SMSException("send sms IOException", e);
        } finally {
            // 关闭连接,释放资源
            try {
                httpclient.close();
            } catch (IOException e) {
                LOGGER.error("close httpclient is IOException", e);
            }
        }
        LOGGER.info("end sms send. accept=[{}]", mobileNumber);
    }

}

猜你喜欢

转载自my.oschina.net/u/3142419/blog/1574153