国外短信发送接口

news/2024/11/28 19:23:01/

最近自己在弄一个英国的优惠券的网站!需要用到国外的短信发送接口!

 

搜索和查找发现有几个比较不错的短信发送接口网站!

 

代码粘出来给大家分享一下!

 

 

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", "*********");}
}

 

大家注意不同的短信接口需要的国家代码格式会有所不同,测试类可以看到。。相关国家代码可以去查看相关网页,百度,谷歌都可以查到。你懂的!另外附上国家代码查看文档!见附件

 

 

 


http://www.ppmy.cn/news/957435.html

相关文章

submail 发送国际短信,国内短信,国际国内邮箱工具类

submail 发送国际短信,国内短信,国际国内邮箱工具类 添加集成sdk <!-- submail邮件集成 --><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId

日本超高人气聊天软件LINE最全注册攻略来了

简介&#xff1a;Line是一款即时的通讯软件&#xff0c;全球注册用户超过4亿&#xff0c;在日本常用的聊天软件就是Line&#xff0c;Line的特点是能够让用户在有网络的情况下&#xff0c;随时随地免费和亲朋好友通话&#xff0c;同时也可以通过Line使用购物&#xff0c;行动支付…

短信平台不知道怎么选?来看看这几个平台:

不少程序员在做项目的时候会碰上短信收发验证码的问题&#xff0c;通常来说解决方案有二&#xff0c;要么自己写一个验证码模块儿&#xff0c;要么去找短信平台。但自己写一个验证码模块是出了名的麻烦&#xff0c;而且会耗费掉不少时间&#xff0c;有着时间到不如优化下自己的…

一文读懂Mysql 优化之order by【百万乃至千万数据查询优化推荐】

一文读懂Mysql 优化之order by 驱动表与被驱动表概念复合索引的三大原则排序方式对比索引排序驱动表排序临时表排序排序算法驱动表与被驱动表概念 当使用left join时,左表是驱动表,右表是被驱动表当使用right join时,右表是驱动表,左表是被驱动表当使用join(inner join) …

ChatGPT-4来袭,留学申请文书面临挑战如何破局?

近日&#xff0c;ChatGPT再次“进化”&#xff0c;其最新版本ChatGPT-4又掀高潮。其生产者OpenAI 称&#xff0c;“ChatGPT-4是最先进的系统&#xff0c;能生产更安全和更有用的回复。”和上一代相比&#xff0c;GPT-4拥有了更广的知识面和更强的解决问题能力&#xff0c;在创意…

【论文学习】Distortion Agnostic Deep Watermarking

一、前言 论文链接&#xff1a;Distortion Agnostic Deep Watermarking 论文主要内容&#xff1a; 该文献提出了一种失真不可知的鲁棒水印模型&#xff0c;以解决现有DNN鲁棒水印方法的局限性。现有的DNN鲁棒水印方法&#xff0c;通常是在训练阶段将各类失真&#xff08;例如…

chatGPT对汽车制造行业有什么帮助

在汽车制造领域&#xff0c;ChatGPT可以发挥以下几个方面的作用&#xff1a; 可以用于汽车设计和优化。通过分析各种数据集&#xff0c;例如车身结构、发动机参数、空气动力学参数等&#xff0c;ChatGPT可以帮助汽车制造商设计和优化汽车结构&#xff0c;提高汽车的性能和燃油效…

最近爆火的ChatGPT核心技术演进历程

文章目录 1.前言2.初识ChatGPT2.1.什么是ChatGPT2.2.ChatGPT和其他模型对比具有的特性 3.ChatGPT技术演进历程3.1.Transformer&#xff08;转移学习&#xff09;和基础模型3.2.GPT-1&#xff1a;简化模型&#xff0c;使其更适合自然语言生成3.2.1.什么是GPT-13.2.1.GPT-1的优势…