当前位置: 移动技术网 > IT编程>开发语言>Java > java实现短信通信的完整教程

java实现短信通信的完整教程

2019年07月22日  | 移动技术网IT编程  | 我要评论

前言

短信信息的发送目前已经是项目中必不可少的部分,我们怎么通过web页面来实现把信息推送到别人手机上呢?简单点,编码的方式简单点!看完本篇文章,以后要实现短信的发送都只需要知道发什么?发给谁?就ok了,代码如下,是不是很简单^_^

string result= "";//返回状态
note note = new note();
string tel = "17089490559";//接收人手机号
string message = "您好!";//短信内容
result = note.sendnote(tel,message);//信息发送状态

要实现在页面或控制台输入一些信息再指定一个电话号码就能把信息发送给对方,这对b/s结构的项目来说可是头疼的,但我们能不能把要发送的信息和要发送的电话号码交给一个负责发送短信的服务器,由它来帮我们完成信息的发送呢?答案是肯定的,只是钱的问题…….(毕竟天下没有免费的午餐),明白了这一点,接下来就好办了,我们只要知道服务器在哪,以及怎么联系服务器不就搞定了。所幸的是apache给我们提供了一个httpclient 子类,用于帮助我们连接到发送短信的服务器,那发送短信的服务器在哪呢?网上有各种各样的短信服务平台,接下来我们以中国网建为例来讲解短信信息的发送。
首先,你需要注册一个账号,注册完成后系统会为你提供一个用户名和一个密钥(在修改短信密钥里找到)并且提供5条免费短信,对于测试来说足够了,我们需要的就是id和key这两个东西,接下来开始吧!

1、导入相关jar包

相关jar包:

jar包下载:

2、制作短信小工具

我们希望的是一劳永逸,做一个项目就要导包改各种参数是很费劲的,接下来我们用单例模式来为我们的项目提供信息修改吧,有信息变动我们只需要改配置文件就可以了,代码部分完全不用变。这样我们通过一个note.properties来放配置文件,一个confignoteinfo.java调用配置文件参数的类,以及一个note.java实现短信的发送的类就实现小工具的制作了,接下来依次教大家配置这三个文件。

第一步,我们在项目里新建一个叫note.properties的文件,里面的参数就两个(这里是放在src根目录下里)。

id=xxx(xxx是你注册的用户名)
key=********(注册成功后平台提供的短信密钥)

第二步,confignoteinfo.java获取配置文件中的id和key

public class confignoteinfo {
 private static confignoteinfo confignoteinfo;
 private static properties properties;

 private confignoteinfo(){
  //note.properties是你在根目录新建的配置文件
  string configfile="note.properties";
  properties=new properties();
  inputstream in=confignoteinfo.class.getclassloader().getresourceasstream(configfile);
  try {
   properties.load(in);
   in.close();
  } catch (ioexception e) {
   e.printstacktrace();
  }
 }
 public static confignoteinfo getinstance(){
  if(confignoteinfo==null){
   confignoteinfo = new confignoteinfo();
  }
  return confignoteinfo;
 }
 public string getstring(string key){

  return properties.getproperty(key);
 }
}

第三步,note.java类实现短信信息的发送,基于oop思想,我们还是把短信发送单独提出来成为一个方法,让以后的工作中只需要传一个手机号和一个消息字符串就能实现短信的发送。(如果是多个手机号可传手机号的数组或list集合)

package cn.hs.tools;

import java.io.ioexception;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.postmethod;

/***
 * @author heshuang
 *@create_date:2017-5-4
 */
public class note {
 //短信接口,传参(手机号,短信内容)即可发送短信。
 public string sendnode(string tel,string message) throws httpexception, ioexception{
  httpclient client = new httpclient(); 
  postmethod post = new postmethod("http://utf8.sms.webchinese.cn"); 
  post.addrequestheader("content-type", 
    "application/x-www-form-urlencoded;charset=utf-8");// 在头文件中设置转码,这里为utf-8 
  namevaluepair[] data = { 
    new namevaluepair("uid", confignoteinfo.getinstance().getstring("id")), // 注册的用户名,我们通过单例模式从配置文件id中读取用户名。
    new namevaluepair("key",confignoteinfo.getinstance().getstring("key")), // 注册成功后,登录网站后得到的密钥 ,同样从配置文件读取。 
    new namevaluepair("smsmob", tel), // 传递过来的手机号码 
    new namevaluepair("smstext", message) // 传递过来的短信内容 
  };
  post.setrequestbody(data); 
  client.executemethod(post);

  header[] headers = post.getresponseheaders(); 
  int flag = post.getstatuscode(); //返回状态,参照api
  system.out.println("statuscode:" + flag); 
  for (header h : headers) { 
   system.out.println("---" + h.tostring()); 
  } 
  string result = new string(post.getresponsebodyasstring().getbytes( 
    "utf-8")); 
  system.out.println(result); 

  string info="";
  if(integer.parseint(result.trim())>0){
   info="发送成功!";
  }
  switch (result.trim()) {
   case "-1":
    info="用户账号不存在!";
    break;
   case "-2":
    info="接口密钥不正确";
    break;
   case "-3":
    info="尊敬的用户,卖血卖肾,请您先把短信费用缴了!";
    break;
   case "-4":
    info="手机号格式不正确!";
    break;
   case "-6":
    info="ip存在限制!";
    break;
   case "-11":
    info="该用户已被禁用!";
    break;
   case "-14":
    info="短信内容存在非法字符!";
    break;
   default:
    break;
  }
 return info;
 }

}


第四步,在其他地方调用我们的工具类实现短信的发送。

string result= "";//返回状态
note note = new note();
string tel = "17089490559";//要发送的手机号
string message = "您好!";//要发送的短信信息
result = note.sendnote();//获取信息发送状态

就此,java实现短信信息的发送功能就完成了,并且我们还把它做成了一个小工具类,信息有变更时,只需要改配置文件就行了,一次编写,处处复制。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网