Java发送手机短信

来源:互联网 发布:少儿学钢琴软件 编辑:程序博客网 时间:2024/05/17 22:56

该程序我使用的是中国网建SMS短信通来实现的,中国网建地址:http://sms.webchinese.cn/default.shtml

具体使用步骤:

1、注册一个账户

2、获取到key

3、查看相关的api,他提供发送短信的方式有gbk和utf8编码的方式

GBK与UTF-8的请求地址

GBK编码发送接口地址
http://gbk.sms.webchinese.cn/?Uid=本站用户名&Key=接口安全密码&smsMob=手机号码&smsText=短信内容 
UTF-8编码发送接口地址:
http://utf8.sms.webchinese.cn/?Uid=本站用户名&Key=接口安全密码&smsMob=手机号码&smsText=短信内容

参数说明:

http://gbk.sms.webchinese.cn/ gbk编码方式请求路径

http://utf8.sms.webchinese.cn utf-8编码方式请求路径

Uid 中国网建的登录用户名

key注册时填写的接口安全密码

smsMob 需要发送的手机号码

smsText 发送的短信内容

具体代码GET与POST请求都实现了

/** * 短信发送工具类 * @author xiaohu * */public class SMSUtil {/** * Get方式发送短信 * @param mobile 发送号码 * @param content 发送内容 * @return 返回响应编码 * @throws Exception  */public static int GetSMSSend(String mobile,String content) throws Exception{//创建HttpClient对象HttpClient httpClient=new HttpClient();//请求URLString url="http://utf8.sms.webchinese.cn/?Uid="+Constant.UID+"&Key="+Constant.KEY+"&smsMob="+mobile+"&smsText="+content;//创建Get请求GetMethod getMethod=new GetMethod(url);//在请求头中设置转码getMethod.addRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");// 使用系统提供的默认恢复策略getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultMethodRetryHandler());//获取头信息数组Header[] header=getMethod.getRequestHeaders();//执行GEt请求httpClient.executeMethod(getMethod);//获取响应码int stateCode=getMethod.getStatusCode();if(stateCode!=HttpStatus.SC_OK){System.out.println("Method fai");}//循环打印出头信息for (Header h : header) {System.out.println(h.toString());}String result=new String(getMethod.getResponseBodyAsString().getBytes("utf-8"));System.out.println(result);return stateCode;}/** * Post请求发送 * @param mobile 发送手机号码 * @param content 发送内容 * @return 返回响应码 */public static int PostSMSSend(String mobile,String content) throws Exception{//创建HttpClient对象HttpClient httpClient=new HttpClient();//请求URLString url="http://utf8.sms.webchinese.cn";//创建POSt请求PostMethod post=new PostMethod(url);//设置请求头信息post.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");//创建参数NameValuePair value1=new NameValuePair("Uid", Constant.UID);NameValuePair value2=new NameValuePair("Key", Constant.KEY);NameValuePair value3=new NameValuePair("smsMob", mobile);NameValuePair value4=new NameValuePair("smsText", content);NameValuePair[] parametersBody={value1,value2,value3,value4};//将参数放入数组post.setRequestBody(parametersBody);//设置POST请求参数//使用系统提供的默认回复策略post.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultMethodRetryHandler());//执行POST方法httpClient.executeMethod(post);//获取响应编码int stateCode=post.getStatusCode();//获取请求头参数信息Header[] header=post.getResponseHeaders();for(Header h:header){System.out.println(h.toString());}String result=new String(post.getResponseBodyAsString().getBytes("UTF-8"));System.out.println(result);return stateCode;}}

定义的常量类

public class Constant {public final static String UID="";//中国网建用户名public final static String KEY="";//短信密钥}

测试

public static void main(String[] args) {String mobile="";//发送手机号码String content="您好";//发送内容try {content=URLEncoder.encode(content, "UTF-8");//将发送内容转码,必须要转,否则请求报错int stateCode=SMSUtil.GetSMSSend(mobile, content);} catch (Exception e) {e.printStackTrace();}}

结果:

如果看到这样的结果则发送成功

如果发送失败则会返回响应的值

这里是从官网上截取下来的。还有一点,如果你想节约你的短信发送数量,建议发送短信内容的长度不要超过70个字,如果超过70个字,则会按照64个字为一条短信计算

实现短信发送还需要3个jar包

到这里这个功能就算实现了





0 1
原创粉丝点击