发短信接口

发短信接口

  1 import java.io.IOException;
  2 import java.io.InputStream;
  3 import java.io.OutputStream;
  4 import java.net.HttpURLConnection;
  5 import java.net.MalformedURLException;
  6 import java.net.URL;
  7 import java.security.MessageDigest;
  8 import java.text.SimpleDateFormat;
  9 import java.util.Date;
 10 import java.util.HashMap;
 11 import java.util.regex.Matcher;
 12 import java.util.regex.Pattern;
 13
 14 import org.slf4j.Logger;
 15 import org.slf4j.LoggerFactory;
 16
 17
 18 public class MessageSend {
 19     //获取当前时间
 20     private static final Logger log = LoggerFactory.getLogger(MessageSend.class);
 21     private static String getnowtime(){
 22         String todayTime=new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss-SSS").format( new Date() );
 23         return todayTime;
 24
 25     }
 26     public static void main(String[] args){
 27         HashMap<String, Object> map = new HashMap();
 28          map.put("password", password);
 29          map.put("password", password);
 30          map.put("epid", epid);
 31          String phone_num="电话号码";
 32          String msgContent="测试消息";
 33
 34         String returnValue=sendMsg(map, phone_num, msgContent);
 35         System.out.println(returnValue);
 36     }
 37
 38     public static String sendurl = "www.fbaidu.com";//发短信URL
 39     public static String recurl = "www.sbaidu.com";
 40     //发送短信
 41     public static String sendMsg(HashMap map,String phone_num,String msgContent){
 42         String returnValue="-1";
 43         try {
 44             String userName = (String)map.get("username");
 45             String passWord = (String)map.get("password");
 46             String epid = (String)map.get("epid");
 47             String[] num = phone_num.split(",");
 48             StringBuffer wrongNum = new StringBuffer();
 49             wrongNum.append("");
 50             boolean checkPhoneNum = true;
 51             for(int i=0;i<num.length;i++){
 52                 checkPhoneNum = checkPhoneNumFormat(num[i]);
 53                 if(!checkPhoneNum){
 54                     wrongNum.append(num[i]);
 55                     wrongNum.append(".");
 56                 }
 57                 checkPhoneNum = true;
 58             }
 59             if(!wrongNum.toString().equals("")){
 60                 throw new Exception("电话号码输入有误["+wrongNum.toString()+"]");
 61             }
 62             StringBuffer sb = new StringBuffer();
 63             sb.append("username=");
 64             sb.append(userName);
 65             sb.append("&");
 66             sb.append("password=");
 67             sb.append(passWord);
 68             sb.append("&");
 69             sb.append("phone=");
 70             sb.append(phone_num);
 71             sb.append("&");
 72             sb.append("message=");
 73             sb.append(msgContent);
 74             sb.append("&");
 75             sb.append("epid=");
 76             String key = "";
 77         //    if(epid != null && !epid.equals(""))
 78         //        key = encodeToMD5(phone_num,8,10,epid);
 79             sb.append(epid);
 80             String linkid = "";
 81             sb.append("&");
 82             sb.append("linkid=");
 83             sb.append(linkid);
 84             String subcode = "";
 85             sb.append("&");
 86             sb.append("subcode=");
 87             sb.append(subcode);
 88             URL http_url = new URL(sendurl);
 89             HttpURLConnection http_conn = (HttpURLConnection)http_url.openConnection();
 90             http_conn.setRequestMethod("GET");
 91             http_conn.setDoOutput(true);
 92             OutputStream outStream = http_conn.getOutputStream();
 93
 94         //    System.out.println(sb.toString());
 95             outStream.write(sb.toString().getBytes());
 96         //    System.out.println(sb.toString().getBytes()+"=sb.toString().getBytes()");
 97             outStream.flush();
 98             outStream.close();
 99
100             InputStream inStream = http_conn.getInputStream();
101             byte[] inByte = new byte[inStream.available()];
102             int offset = 0;
103
104             while(offset < inByte.length){
105                 offset += inStream.read(inByte);
106             }
107             inStream.close();
108             http_conn.disconnect();
109             returnValue = new String(inByte);
110             //System.out.println("inByte="+inByte.length);
111         //    System.out.println("inStream.read(inByte)="+inStream.read(inByte));
112             if(returnValue.equals("00")){//
113                 log.debug("短信发送成功");
114             }else if(returnValue.equals("1")){//
115                 log.debug("短信发送失败,原因:参数不完整!");
116             }else if(returnValue.equals("2")){//
117                 log.debug("短信发送失败,原因:鉴权失败(包括:用户状态不正常、密码错误、用户不存在、地址验证失败,黑户)");
118             }else if(returnValue.equals("3")){//
119                 log.debug("短信发送失败,原因:号码数量超出50条");
120             }else if(returnValue.equals("4")){//
121                 log.debug("短信发送失败,原因:发送失败");
122             }else if(returnValue.equals("5")){//
123                 log.debug("短信发送失败,原因:余额不足");
124             }else if(returnValue.equals("6")){//
125                 log.debug("短信发送失败,原因:发送内容含屏蔽词");
126             }else {
127                 System.out.println(returnValue);
128                 log.debug("短信发送失败:原因"+returnValue);
129             }
130         } catch (MalformedURLException e){
131             e.printStackTrace();
132         } catch (IOException e){
133             e.printStackTrace();
134         } catch(Exception e){
135             e.printStackTrace();
136         }
137         return returnValue;
138     }
139     private static boolean isLogin = false;
140
141     public static String encodeToMD5(String phone_num, int firstFixd, int lastFixd, Number epid){
142         StringBuffer sb = new StringBuffer();
143         try {
144             if(null == phone_num)
145                 throw new NullPointerException();
146             String[] num = phone_num.split(",");
147             String key = num[0].substring(0, firstFixd)
148                     + num[num.length -1].substring(num[num.length -1].length() - lastFixd) + epid;
149             MessageDigest msgd = MessageDigest.getInstance("MD5");
150             msgd.update(key.getBytes());
151             byte[] md5 = msgd.digest();
152             for(int i=0;i < md5.length; i++){
153                 if((md5[i] & 0xFF) < 10)
154                     sb.append("0");
155                 sb.append(Integer.toString(md5[i] & 0xFF, 16));
156             }
157         } catch (Exception e) {
158             e.printStackTrace();
159         }
160         System.out.println(sb.toString().toUpperCase());
161         return sb.toString().toUpperCase();
162     }
163     //验证手机号
164     public static boolean checkPhoneNumFormat(String phone_num){
165         boolean flag = false;
166         String regex = "^(13[0-9]|15[0-9]|18[0-9])\\d{8}$";
167         Pattern pattern = Pattern.compile(regex);
168         Matcher matcher = pattern.matcher(phone_num);
169         if(matcher.find()){
170             flag = true;
171         }
172         return flag;
173     }
174
175
176 }
时间: 2025-01-15 08:41:23

发短信接口的相关文章

ios打电话发短信接口

电话.短信是手机的基础功能,iOS中提供了接口,让我们调用.这篇文章简单的介绍一下iOS的打电话.发短信在程序中怎么调用. 1.打电话 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://10010"]];//打电话 使用openURL这个API打电话结束后,返回的是系统的拨打电话界面,如何才能返回自己的应用呢?有两种方法与大家分享. 第一种是用UIWebView加载电话,这种是合法的,可以上A

iOS开发之调用系统打电话发短信接口以及程序内发短信

在本篇博客开头呢,先说一下写本篇的博客的原因吧.目前在做一个小项目,要用到在本应用程序内发验证码给其他用户,怎么在应用内发送短信的具体细节想不大起来了,于是就百度了一下,发现也有关于这方面的博客,点进去看了看,个人感到有点小失望,写的太不详细,只是简单的代码罗列,而且代码也没注释,大概是因为太简单了吧.今天在做完项目的发短信功能后感觉有必要把这部分内容整理一下,做个纪念也是好的不是吗.废话少说,切入今天的正题.下面的发短信,打电话当然需要真机测试了. 一.调用系统功能 在iOS中打开系统本身的打

利用阿里大于接口发短信(Delphi版)

阿里大于是阿里通信旗下产品,融合了三大运营商的通信能力,提供包括短信.语音.流量直充.私密专线.店铺手机号等个性化服务.每条四毛五,价钱还算公道,经老农测试,响应速度非常快,基本上是秒到.官方文档提供了以下语言的 Demo JAVA .NET PHP Python CURL C/C++ NodeJS 唯独没有 Dephi,这不能怪马云,毕竟 Delphi 实在太小众了. 最近用 Delphi 写个 App,注册用户需要用到手机短信验证,于是找到的阿里大于,使用 Delphi 10.1 berli

c#短信接口代码实现(发短信)

我这里以56短信接口为例! 首先需要到56短信网上去注册个账号,代码中需要用到4个参数:企业ID.用户名.密码.所用平台 在里面充钱配合下面的代码就可以发送短信啦! public void Send(string orderIDs, string mobile, string productName)    { //下文中的tel:就是要发送的手机号码 //msg:要发送的内容        //发送短信        string cid = "1345";//企业号ID      

Java通过SMS短信平台实现发短信功能

在项目中使用过发短信的功能,但那个由于公司内部的限制很麻烦,今天在网上找到一个简单的,闲来无事就把它记录如下: 本程序是通过使用中国网建提供的SMS短信平台实现的(该平台目前为注册用户提供5条免费短信,3条免费彩信,这足够用于我们测试用了.在使用前需要注册,注册地址为http://sms.webchinese.cn/reg.shtml),下面是程序源码: /** * @Author dengsilinming * @Date 2012-9-18 * */ package com.dengsili

关于手机短信接口优化

项目:目前需要支持手机号码注册,流程如下: 1)用户输入手机号码 2)点击获取手机校验码 3)收到短信息后,填入验证码.完成注册 有个问题,在项目中前期设计问题,导致短信接口被恶意调用. 调整方案: 网络提供方案: 推荐的对接方式:1.流程限定--将手机短信验证和用户名密码设置分成两个步骤,用户在注册成功用户名密码后,下一步才进行手机短信验证.(推荐)2.绑定图型校验码--将图形校验码和手机验证码进行绑定,这样能比较有效的防止软件恶意点击.(推荐) 不推荐的对接方式:3.短信发送间隔设置--设置

阿里大鱼短信接口

阿里大于短信验证实现完整代码分享 http://bbs.2ccc.com/topic.asp?topicid=515649 补充:跨平台的例子可以去csdn下载这位大神的demo http://download.csdn.net/detail/hansxia888/9603061 1 这两天搞那个sign,搞的烦躁,问了一下论坛的那位同学,开口500. 2 唉,delphi这种偏门的就是这样,啥都贵. 3 后来想想用C#按阿里那边的帮助文档写了个,可以正常发送.然后把C#翻译成Delphi就可以

调用系统的打电话,发短信,邮件,蓝牙

在开发某些应用时可能希望能够调用iOS系统内置的电话.短信.邮件.浏览器应用,此时你可以直接使用UIApplication的OpenURL:方法指定特定的协议来打开不同的系统应用.常用的协议如下: 打电话:tel:或者tel://.telprompt:或telprompt://(拨打电话前有提示) 发短信:sms:或者sms:// 发送邮件:mailto:或者mailto:// 启动浏览器:http:或者http:// 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

如何选择一家好的验证码短信接口服务商?

多年的行业经验及与客户沟通中所了解的一些信息反馈.现在总结出来 跟大家说一说,怎样去选择一家比较好的短信接口提供商. 公司网站/app想嵌入短信接口 发验证短信.去网上一搜几十家,每个网站的产品宣传.介绍都大同小异,挑了几家问问价格低至3,4分,高至7,8分.价格高的会说是验证码专用通道,但又去问问价格低的 也说是专用 这时在没使用过的情况下 价格低的完全占绝对优势 因为70%客户会选择价格便宜的.于是乎就开了帐号 充值使用了,用了3天,一个礼拜或者1-2个月 渐渐的问题开始多了,收不到 延迟厉