one:
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; /** * SMS sending interface */ @PropertySource(value = "sms.properties", encoding = "utf-8") @Service public class SMSSenderService { private Logger LOGGER = LoggerFactory.getLogger (SMSSenderService.class); //request url @Value("${sms.url}") private String url; //Institution username, provided by SMS provider @Value("${sms.username}") private String username; //Organization password, provided by SMS provider @Value("${sms.password}") private String password; // SMS signature @Value("${sms.sige}") private String sign; /** * Send a single SMS * * @param mobileNumber mobile number * @param content SMS content */ public void send(String mobileNumber, String content) { LOGGER.info("start into sms send. accept=[{}]", mobileNumber); // Create a default httpClient instance. CloseableHttpClient httpclient = HttpClients.createDefault(); // create httppost HttpPost httppost = new HttpPost(url); // create parameter queue 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("SMS unknown error, 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 { // close the connection and release resources try { httpclient.close(); } catch (IOException e) { LOGGER.error("close httpclient is IOException", e); } } LOGGER.info("end sms send. accept=[{}]", mobileNumber); } }