最近自己在弄一个英国的优惠券的网站!需要用到国外的短信发送接口!
搜索和查找发现有几个比较不错的短信发送接口网站!
代码粘出来给大家分享一下!
SMSService 抽象接口类
/*** squarelife* * @author eric** 2011-11-28 下午08:41:43*/ package com.life.service.sms;/*** @author eric** 2011-11-28 下午08:41:43*/ public interface SMSService {boolean sendMessage(String msg,String countryCode,String phoneNumber); }
需要配置的短信配置信息! sms.properties#短信接口名称Gateway160SMSServiceImpl,BulksmsSmsServiceImpl
#Gateway160SMSServiceImpl,test123456,test123456, 065af763-bdb8-4f27-bc81-d6c14f2f545c
#http://www.bulksms.co.uk/
#http://www.gateway160.com/
wy.sms.service.name=Gateway160SMSServiceImpl
#短信接口的用户名
wy.sms.service.accountName=****
#短信接口国家代码
#Gateway160SMSServiceImpl 用国家代码如中国:CN 美国:US 英国:GB
#BulksmsSmsServiceImpl 用电话代码如中国:86 美国:1 英国:44
wy.sms.service.countryCode=CN
#短信接口的密码
wy.sms.service.password=****
#短信接口APIkey 注册时候的APIkey
wy.sms.service.apiKey=****
package com.life.service.sms;import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;import com.life.util.PropertyUtil;
/*** * @author eric** 2011-11-28 下午09:15:23*/
public class AbstractSMSService {/*** 获取属性值* 2011-11-28 下午09:15:27* @param key* @return*/protected String getProperty(String key) {return new PropertyUtil().getProperty(key); }/*** 2011-11-28 下午10:28:57* @param params* @return* @throws UnsupportedEncodingException*/protected String encode(final HashMap<String,String> params) throws UnsupportedEncodingException {StringBuilder sb = new StringBuilder(); for (Map.Entry<String, String> entry : params.entrySet()) {String key = entry.getKey();String val = entry.getValue();sb.append(URLEncoder.encode(key, "UTF-8"));sb.append("=");sb.append(URLEncoder.encode(val, "UTF-8"));sb.append("&");} return sb.toString();}
}
BulksmsSmsServiceImpl 短信接口实现类 接口相关信息网址http://www.bulksms.co.uk/
/*** squarelife* * @author eric** 2011-11-28 下午09:33:18*/
package com.life.service.sms.impl;import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;import org.apache.log4j.Logger;import com.life.service.sms.AbstractSMSService;
import com.life.service.sms.SMSService;/*** @author eric* * 2011-11-28 下午09:33:18*/
public class BulksmsSmsServiceImpl extends AbstractSMSService implementsSMSService {private static final Logger logger = Logger.getLogger(BulksmsSmsServiceImpl.class);/** (non-Javadoc)* * @see com.life.service.sms.SMSService#sendMessage(java.lang.String,* java.lang.String, java.lang.String)*/@Overridepublic boolean sendMessage(String msg, String countryCode,String phoneNumber) {HashMap<String, String> params = new HashMap<String, String>();params.put("username", getProperty("wy.sms.service.accountName"));params.put("password", getProperty("wy.sms.service.password"));params.put("phoneNumber", phoneNumber);params.put("message", msg);params.put("want_report", "1");params.put("msisdn", countryCode + phoneNumber);URL url = null;HttpURLConnection httpCon = null;try {// Construct data// String data = "";/** Note the suggested encoding for certain parameters, notably the* username, password and especially the message. ISO-8859-1 is* essentially the character set that we use for message bodies,* with a few exceptions for e.g. Greek characters. For a full list,* see:* http://www.bulksms.co.uk/docs/eapi/submission/character_encoding/*/// Send dataurl = new URL("http://www.bulksms.co.uk:5567/eapi/submission/send_sms/2/2.0");/** If your firewall blocks access to port 5567, you can fall back to* port 80: URL url = new* URL("http://www.bulksms.co.uk/eapi/submission/send_sms/2/2.0");* (See FAQ for more details.)*/httpCon = (HttpURLConnection) url.openConnection();httpCon.setDoOutput(true);httpCon.setRequestMethod("POST");httpCon.setRequestProperty("Content-type", "text/html");httpCon.setRequestProperty("Accept-Charset", "UTF-8");httpCon.setRequestProperty("contentType", "UTF-8");// write post bodyOutputStreamWriter out = new OutputStreamWriter(httpCon.getOutputStream(), "UTF-8");String postBody = encode(params);out.write(postBody);out.flush();out.close();// Get the responseStringBuilder sb = new StringBuilder();BufferedReader rd = new BufferedReader(new InputStreamReader(httpCon.getInputStream()));String line;while ((line = rd.readLine()) != null) {sb.append(line);}rd.close();logger.info("responseCode=" + httpCon.getResponseCode());logger.info("responseBody=" + sb.toString());if (httpCon != null&& httpCon.getResponseCode() == HttpURLConnection.HTTP_OK) {return true;} else {return false;}} catch (Exception e) {logger.error("Exception throw!", e);return false;} finally {if (httpCon != null) {httpCon.disconnect();}}}}
Gateway160SMSServiceImpl 接口相关信息网址http://www.gateway160.com/
/*** squarelife* * @author eric** 2011-11-28 下午08:43:23*/
package com.life.service.sms.impl;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;import org.apache.log4j.Logger;import com.life.service.sms.AbstractSMSService;
import com.life.service.sms.SMSService;/*** @author eric* * 2011-11-28 下午08:43:23*/
public class Gateway160SMSServiceImpl extends AbstractSMSService implementsSMSService {private static final Logger logger = Logger.getLogger(Gateway160SMSServiceImpl.class);@Overridepublic boolean sendMessage(String msgContent, String countryCode,String phoneNumber) {HashMap<String, String> params = new HashMap<String, String>();params.put("accountName", getProperty("wy.sms.service.accountName"));params.put("key", getProperty("wy.sms.service.apiKey"));params.put("phoneNumber", phoneNumber);params.put("message", msgContent);params.put("countryCode", countryCode);try {URL url = new URL("http://api.gateway160.com/client/sendmessage/");HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();httpCon.setDoOutput(true);httpCon.setRequestMethod("POST");// write post bodyOutputStreamWriter out = new OutputStreamWriter(httpCon.getOutputStream());String postBody = encode(params);out.write(postBody);out.flush();out.close();// Get the responseStringBuilder sb = new StringBuilder();BufferedReader rd = new BufferedReader(new InputStreamReader(httpCon.getInputStream()));String line;while ((line = rd.readLine()) != null) {sb.append(line);}rd.close();logger.info("responseCode=" + httpCon.getResponseCode());logger.info("responseBody=" + sb.toString());if (httpCon != null&& httpCon.getResponseCode() == HttpURLConnection.HTTP_OK) {logger.info("连接成功!");if (sb != null && sb.equals("1")) {logger.info("短信发送成功!");return true;} else if (sb != null && sb.equals("0")) {logger.info("invalid account name or key");return false;} else if (sb != null && sb.equals("-1")) {logger.info("短信发送失败!");return false;} else {return false;}} else {logger.info("连接失败!");return false;}} catch (IOException e) {logger.error("IOException thrown! ", e);return false;}}}
给出测试类!相关的PropertiesUtil.java文件就不提供了。一般大家都会写吧!如果有实在有问题的可以联系我!
package com.life.test;import org.junit.Test;import com.life.service.sms.SMSService;
import com.life.service.sms.impl.BulksmsSmsServiceImpl;
import com.life.service.sms.impl.Gateway160SMSServiceImpl;/*** * @author eric** 2011-11-28 下午10:01:35*/
public class SMSTest {@Testpublic void testSendMsg(){
// SMSService smsService=new BulksmsSmsServiceImpl();
// smsService.sendMessage("亲爱的,我爱你!", "86", "**********");
//
// SMSService smsService=new Gateway160SMSServiceImpl();
// smsService.sendMessage("亲爱的,我爱你! ", "CN", "**********");
//
// SMSService smsService=new Gateway160SMSServiceImpl();
// smsService.sendMessage("哈哈,你这么肯定是我发的呀,你好好上课吧,不打扰你上课了,上课加油!!三明治加油!!", "CN", "*******");SMSService smsService=new Gateway160SMSServiceImpl();smsService.sendMessage("你莫冤枉我撒,又冤枉我,老是冤枉我,我承认个啥呀,嘎", "CN", "*********");}
}
大家注意不同的短信接口需要的国家代码格式会有所不同,测试类可以看到。。相关国家代码可以去查看相关网页,百度,谷歌都可以查到。你懂的!另外附上国家代码查看文档!见附件