JAVA发送手机短信

来源:互联网 发布:图书管理系统源码 编辑:程序博客网 时间:2024/04/30 07:26
JAVA发送手机短信,流传有几种方法:(1)使用webservice接口发送手机短信,这个可以使用sina提供的webservice进行发送,但是需要进行注册;(2)使用短信mao的方式进行短信的发送,这种方式应该是比较的常用,前提是需要购买硬件设备,呵呵;</span></p>
(3)使用中国网建提供的SMS短信平台,我的这个小的demo,是基于这个行是发送的.</span></p>

说明:java实现了发送手机短信<br> * 作者:aa00aa00<br>
/**
* 说明: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();
}
}


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