首页 > 编程 > Java > 正文

java实现发送手机短信

2019-11-26 14:28:23
字体:
来源:转载
供稿:网友

本文主要研究了JAVA语言发送手机,分享给大家,供大家参考,具体内容如下

JAVA发送手机短信,流传有几种方法:

(1)使用webservice接口发送手机短信,这个可以使用sina提供的webservice进行发送,但是需要进行注册;
(2)使用短信mao的方式进行短信的发送,这种方式应该是比较的常用,前提是需要购买硬件设备,呵呵;
(3)使用中国网建提供的SMS短信平台,我的这个小的demo,是基于这个行是发送的。 

说明:java实现发送手机短信

/*** 说明:java实现发送手机短信* 作者:aa00aa00*/package com.test.mobile; import org.apache.commons.httpclient.Header;import org.apache.commons.httpclient.HttpClient;import org.apache.commons.httpclient.NameValuePair;import org.apache.commons.httpclient.methods.PostMethod; public class SendMsg_webchinese { public static void main(String[] args) throws Exception { HttpClient client = new HttpClient();PostMethod post = new PostMethod("http://sms.webchinese.cn/web_api/");post.addRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=gbk");// 在头文件中设置转码NameValuePair[] data = { new NameValuePair("Uid", "*****"), // 注册的用户名new NameValuePair("Key", "*******"), // 注册成功后,登录网站使用的密钥new NameValuePair("smsMob", "*********"), // 手机号码new NameValuePair("smsText", "java程序发的信息!!") };post.setRequestBody(data); client.executeMethod(post);Header[] headers = post.getResponseHeaders();int statusCode = post.getStatusCode();System.out.println("statusCode:" + statusCode);for (Header h : headers) {System.out.println(h.toString());}String result = new String(post.getResponseBodyAsString().getBytes("gbk"));System.out.println(result);post.releaseConnection();}}  

运行以上的代码:就可以给自己的手机发送短信了,本人亲测,没有问题,分享给大家!

以上就是本文的全部内容,希望对大家学习java程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表