java 调用运营商短信方法(一)
一、编写短信服务器端代码:
package com.enjoy.base.common.util; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; import java.net.URLDecoder; import java.net.URLEncoder; public class SmsClient { protected String smsSvcUrl = "************"; //服务器URL 地址 protected String cust_code = "*******"; //账号 protected String password = "******"; //密码 protected String sp_code = "**********"; //接入码(扩展码) public void sendSms(String mobiles, String content) throws IOException { sendSms(mobiles, content, sp_code, 0); } public void sendSms(String mobiles, String content, long task_id) throws IOException { sendSms(mobiles, content, sp_code, task_id); } public void sendSms(String mobiles, String content, String sp_code) throws IOException { sendSms(mobiles, content, sp_code, 0); } public void sendSms(String mobiles, String content, String sp_code, long task_id) throws IOException { String urlencContent = URLEncoder.encode(content,"utf-8"); //String sign = MD5.getMD5((urlencContent + password).getBytes()); // String sign=MD5.sign(urlencContent, password, "utf-8"); String sign = MD5NewUtil.sign(urlencContent+password); System.out.println(sign); String mobiles_utf8 = URLEncoder.encode(mobiles, "UTF-8"); String postData = "content=" + urlencContent + "&destMobiles=" + mobiles_utf8 + "&sign=" + sign + "&cust_code=" + cust_code + "&sp_code=" + sp_code + "&task_id=" + task_id; System.err.println(postData); URL myurl = new URL(smsSvcUrl); URLConnection urlc = myurl.openConnection(); urlc.setReadTimeout(1000 * 30); urlc.setDoOutput(true); urlc.setDoInput(true); urlc.setAllowUserInteraction(false); DataOutputStream server = new DataOutputStream(urlc.getOutputStream()); //System.out.println("发送数据=" + postData); server.write(postData.getBytes("utf-8")); server.close(); BufferedReader in = new BufferedReader(new InputStreamReader( urlc.getInputStream(), "utf-8")); String resXml = "", s = ""; while ((s = in.readLine()) != null) resXml = resXml + s + "\r\n"; in.close(); System.out.println("接收数据=" + URLDecoder.decode(resXml,"utf-8")); } public static void main(String[] args) { SmsClient client = new SmsClient(); try { client.sendSms("手机号", "【公司名称】短信内容。"); } catch (IOException e) { e.printStackTrace(); } } }
二、在服务层方法调用
for(Object obj : userlist){ Object[] objarry = (Object[]) obj; String userName=(String) objarry[0]; String mobile=(String) objarry[1]; String url = sbCodeUtils.getCodeDefMapNV("SYS_URL").get("URL_JDC_PUSH_BJ_P"); url+="?userName="+userName+"&msg="+Hex64EncodeUtils.stringToUnicode(content); System.out.println(url); //发送短信 try { UrlUtils.getConnectionStr(url, "utf-8", 10000); if(!StringUtil.isEmpty(mobile)){ SmsClient client = new SmsClient(); client.sendSms(mobile, "您有一条来自"+useradmin.getNickname()+"的报警信息,请及时处理。"); } } catch (IOException e) { e.printStackTrace(); } }
三、测试短信
相关推荐
java代码实现发送短信,中国电信smgp协议api,公司在做一个给用户手机发送短信的功能
短信猫其实是一种支持GSM无线通讯的工业级调制解调器,一般基于法国WAVECOM或德国SIEMENS(西门子)GSM模块,插入国内移动通信运营商的SIM卡后即可接入运营商GSM网络,实现无线GSM通话、短信、数据等功能。...
该能力通过运营商级优质服务通道,面向全国全网用户免费发送短信验证码, 24小时不间断发送,100%PUSH即时到达。 为了保证短信验证码的安全性,下发短信验证码之前,需要首先获取信任码,信任码有效期3分钟,且使用...
支持联通和移动手机,发送时无需关心目标手机运营商,自动识别移动和联通。整体成功率约90%左右。 发送性能优异稳定 API使用的网关服务器拥有多线程发送能力,每秒可同时处理数十个客户端的并发请求。 ...
1.sgip 华为smproxy调用样例 2.短消息代理(SGIP SMProxy)使用说明书.doc 3.smproxy.jar的源码 4.测试通过,可以发送
短信服务,是电信运营商(电信,移动,联通)提供的,仅仅利用java API是无法实现短信服务的,包括之前的邮件发送,java只是一种开发语言,想要实现这些邮件服务,短信服务,我们必须调用接口。
) 联网收费视用户电话运营商的收费标准决定 手机或者无线信号不好时,可能无法连接或更新天气情况 预期读者和阅读建议 预期读者:会使用已经安装安卓系统的手机或者平板电脑及其他移动设备的用户 阅读建议:读者应...
联网收费视用户电话运营商的收费标准决定 2 手机或者无线信号不好时,可能无法连接或更新天气情况 3. 预期读者和阅读建议 1. 预期读者:会使用已经安装安卓系统的手机或者平板电脑及其他移动设备的用户 3 阅读建议...
在Java语言中重要的两个以SOAP技术开始的网络服务框架XFire和Axis也把REST作为自己的另一种选择。它们的新的项目分别是ApacheCXF和Axis2.Java语言也制定关于REST网络服务规范:JAX-RS:JavaAPIforRESTfulWebServices...