今天闲来无事,在微博上看到一个关于用java实现的一个发送手机短信的程序,看了看,写的不太相信,闲的没事,把他整理下来,以后可能用得着
java发送手机短信,流传有几种方法:(1)使用webservice接口发送手机短信,这个可以使用sina提供的webservice进行发送,但是需要进行注册;(2)使用短信mao的方式进行短信的发送,这种方式应该是比较的常用,前提是需要购买硬件设备,呵呵(3)使用中国网建提供的sms短信平台(申请账号地址:http://sms.webchinese.cn/default.shtml)
本程序主要是运用了中国网建提供的sms短信平台,这个短信平台基于java提供个专门的接口,话不多说。,上代码,有代码有真相,呵呵
[java] view
plain copy
print?
package com.text;
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", "cshxxxxxxxx"), // 注册的用户名
new namevaluepair("key", "53295058d1c46710666a"), // 注册成功后,登录网站使用的密钥
new namevaluepair("smsmob", "187xxxxxxx"), // 手机号码
new namevaluepair("smstext", "以后给我老实点哈。。。。听话。。。") };//设置短信内容
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();
}
运行本程序首先的代入三个jar包:
commons-codec-1.4
commons-httpclient-3.1
commons-logging-1.1.1
请自行下载,呵呵
gbk编码发送接口地址:
http://gbk.sms.webchinese.cn/?uid=本站用户名&key=接口安全密码&smsmob=手机号码&smstext=短信内容
utf-8编码发送接口地址:
http://utf8.sms.webchinese.cn/?uid=本站用户名&key=接口安全密码&smsmob=手机号码&smstext=短信内容
获取短信数量接口地址(utf8):
http://sms.webchinese.cn/web_api/sms/?action=sms_num&uid=本站用户名&key=接口安全密码
获取短信数量接口地址(gbk):
http://sms.webchinese.cn/web_api/sms/gbk/?action=sms_num&uid=本站用户名&key=接口安全密码
短信发送后返回值
说 明
-1
没有该用户账户
-2
密钥不正确(不是用户密码)
-3
短信数量不足
-11
该用户被禁用
-14
短信内容出现非法字符
-41
手机号码为空
-42
短信内容为空
大于0
短信发送数量
注:上面的用户名和密码是我原先申请的,不知道为什么被停用了,在运行本程序之前请先到sms短信平台去申请一个用户名和密码。
附:
1. asp 调用例子
<%
'常用函数
'输入url目标网页地址,返回值gethttppage是目标网页的html代码
function gethttppage(url)
dim http
set http=server.createobject("msxml2.xmlhttp")
http.open "get",url,false
http.send()
if http.readystate<>4 then
exit function
end if
gethttppage=bytestobstr(http.responsebody,"gb2312")
set http=nothing
if err.number<>0 then err.clear
end function
function bytestobstr(body,cset)
dim objstream
set objstream = server.createobject("adodb.stream")
objstream.type = 1
objstream.mode =3
objstream.open
objstream.write body
objstream.position = 0
objstream.type = 2
objstream.charset = cset
bytestobstr = objstream.readtext
objstream.close
set objstream = nothing
'自已组合一下提交的url加入自己的账号和密码
sms_url="http://sms.webchinese.cn/web_api/?uid=账号&key=接口密钥&smsmob=手机号码&smstext=短信内容"
response.write gethttppage(sms_url)
%>
2.c# 调用
//需要用到的命名空间
using system.net;
using system.io;
using system.text;
//调用时只需要把拼成的url传给该函数即可。判断返回值即可
public string gethtmlfromurl(string url)
{
string strret = null;
if(url==null || url.trim().tostring()=="")
return strret;
}
string targeturl = url.trim().tostring();
try
httpwebrequest hr = (httpwebrequest)webrequest.create(targeturl);
hr.useragent = "mozilla/4.0 (compatible; msie 6.0; windows nt 5.1)";
hr.method = "get";
hr.timeout = 30 * 60 * 1000;
webresponse hs = hr.getresponse();
stream sr = hs.getresponsestream();
streamreader ser = new streamreader(sr, encoding.default);
strret = ser.readtoend();
catch (exception ex)
strret = null;
3.java调用
import java.io.unsupportedencodingexception;
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://gbk.sms.webchinese.cn");
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","短信内容")};
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();
jar包下载
commons-logging-1.1.1.jar
commons-httpclient-3.1.jar
commons-codec-1.4.jar
4.php
$url='http://sms.webchinese.cn/web_api/?uid=账号&key=接口密钥&smsmob=手机号码&smstext=短信内容';
echo get($url);
function get($url)
if(function_exists('file_get_contents'))
$file_contents = file_get_contents($url);
else
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, curlopt_url, $url);
curl_setopt ($ch, curlopt_returntransfer, 1);
curl_setopt ($ch, curlopt_connecttimeout, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
return $file_contents;
5.vb.net
'调用发送短信,nolist接收号码.多个之间用,分开,memo内容70字
public function sendsms(byval nolist as string, byval memo as string) as string
dim url as string = "http://sms.webchinese.cn/web_api/?uid=账号&key=接口密钥&smsmob=手机号码&smstext=短信内容"
dim webclient as new net.webclient()
'dim responsedata as byte() =
dim srcstring as string = webclient.downloadstring(url)
return srcstring
catch
return "-444"
end try