欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java通过SMS短信平台实现发短信功能 含多语言

程序员文章站 2024-03-13 11:53:27
java通过sms短信平台实现发短信功能  在项目中使用过发短信的功能,但那个由于公司内部的限制很麻烦,今天在网上找到一个简单的,闲来无事就把它记录如下:...

java通过sms短信平台实现发短信功能 

在项目中使用过发短信的功能,但那个由于公司内部的限制很麻烦,今天在网上找到一个简单的,闲来无事就把它记录如下:

本程序是通过使用中国网建提供的sms短信平台实现的(该平台目前为注册用户提供5条免费短信,3条免费彩信,这足够用于我们测试用了。在使用前需要注册,注册地址为http://sms.webchinese.cn/reg.shtml),下面是程序源码: 

 /** 
* @author dengsilinming 
* @date 2012-9-18 
* 
*/ 
package com.dengsilinming.mail; 

import java.io.ioexception; 
import org.apache.commons.httpclient.header; 
import org.apache.commons.httpclient.httpclient; 
import org.apache.commons.httpclient.httpexception; 
import org.apache.commons.httpclient.namevaluepair; 
import org.apache.commons.httpclient.methods.postmethod; 

public class sendmsg_webchinese { 
 
/** 
* @author dengsilinming 
* @date sep 18, 2012 
* @time 9:38:25 am 
* @param args 
* @throws ioexception 
* @throws httpexception 
* @description 
*/ 
public static void main(string[] args) throws httpexception, ioexception { 
httpclient client = new httpclient(); 
postmethod post = new postmethod("http://gbk.sms.webchinese.cn"); 
// 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", "dengsilinming"),// 注册的用户名 
new namevaluepair("key", "72da78da5ff54f450505"),// 注册成功后,登录网站后得到的密钥 
new namevaluepair("smsmob", "12345678900"),// 手机号码 
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); 
 
 } 
 
}  

需要用到的jar包共三个:
 commons-logging-1.1.1.jar
 commons-httpclient-3.1.jar
 commons-codec-1.4.jar 

以下内容摘自中国建网sms短信通api : 

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=接口安全密码 

提示:http调用url接口时, 参数值必须url编码后再调用 

Java通过SMS短信平台实现发短信功能 含多语言

多个手机号请用半角,隔开,如:13888888886,13888888887,1388888888 一次最多对50个手机发送
短信内容支持长短信,最多300个字,普通短信70个字/条,长短信64个字/条计费 

Java通过SMS短信平台实现发短信功能 含多语言

下面是不同的语言调用sms接口的简单demo:
 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
 end function
 
'自已组合一下提交的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;
 }
 return strret;
 }

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()
 try
 'dim responsedata as byte() = 
 dim srcstring as string = webclient.downloadstring(url)
 return srcstring
 catch
 return "-444"
 end try
 end function

 经过测试上面java源码是能够发送成功的,其它语言的没有测试。

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