程序调用飞信API发送免费短信(JAVA例子,其他语言一样用)

<p style="margin: 1em 0px 0.5em; padding: 0px;"><span><span style="line-height: 21px; font-family: arial, sans-serif; font-size: 14px;">网上看到有网页版的飞信,<a style="color: #336699; text-decoration: none;" href="http://fetionlib.appspot.com/">http://fetionlib.appspot.com/</a><span></span>可以添加好友,群发和定时发送短信给飞信好友,还开放了API接口供程序调用,可以用它来监控机器是否正常服务定期给管理员发短信,或者小规模的网站给会员发短信之类的服务。</span></span></p>
<p style="margin: 1em 0px 0.5em; padding: 0px;"><span><span class="Apple-style-span"><span class="Apple-style-span" style="font-family: arial, sans-serif; color: #ff0000; font-size: 14px;"><strong>重要提示:近期所有appspot的https都惨遭RESET,请先前使用API的用户,把调用地址中https改成http</strong></span></span></span></p>
<p style="margin: 1em 0px 0.5em; padding: 0px;"><span><span style="line-height: 21px; font-family: arial, sans-serif; font-size: 14px;">简单测试一下:<a style="color: #336699; text-decoration: none;" href="http://fetionlib.appspot.com/restlet/fetion/13812345678/password/13912345678/message" target="_blank">http://fetionlib.appspot.com/restlet/fetion/<strong>13812345678</strong>/<strong>password</strong>/<strong>13912345678</strong>/<strong>message</strong></a></span></span></p>
<p style="margin: 1em 0px 0.5em; padding: 0px;"><span><span style="line-height: 21px; font-family: arial, sans-serif; font-size: 14px;">其中13812345678 和13912345678是发送方和接收方的手机号码,注意这两个号码必须相互是好友,我们把它换成自己的手机号码,让它发送给自己,password改成您的密码,在浏览器里输入该好后的链接,稍等片刻,如果成功,便会显示OK,此时您的手机应该能收到自己发来的message。</span></span></p>
<p style="margin: 1em 0px 0.5em; padding: 0px;"><span><span style="line-height: 21px; font-family: arial, sans-serif; font-size: 14px;">好了,上面是最简单的测试,废话不多说了,下面放<span class="hilite2">Java</span>调用的例子,来实现发送短信,发送和取消定时短信,加好友等一系列动作。其他语言应该类似的调用Http Connection用GET或POST去实现。下面</span></span><span><span style="line-height: 21px; font-family: arial, sans-serif; font-size: 14px;">例子上是发短信的,其他的如取得好友列表、发送和取消定时短信、添加好友的例子请直接查看<a href="http://fetionlib.appspot.com/api.html">http://fetionlib.appspot.com/api.html</a></span></span></p>
<p style="margin: 1em 0px 0.5em; padding: 0px;"><span style="font-family: arial, sans-serif; font-size: medium;"><span style="line-height: 21px; font-size: 14px;"><span style="line-height: normal;">packagecom.test;<br><br>import<span class="hilite2">java</span>.io.BufferedReader;<br>import<span class="hilite2">java</span>.io.DataOutputStream;<br>import<span class="hilite2">java</span>.io.InputStreamReader;<br>import<span class="hilite2">java</span>.net.HttpURLConnection;<br>import<span class="hilite2">java</span>.net.URL;<br>import<span class="hilite2">java</span>.net.URLEncoder;<br>import<span class="hilite2">java</span>.util.UUID;<br><br>importorg.apache.commons.logging.Log;<br>importorg.apache.commons.logging.LogFactory;</span></span></span></p>
<p style="margin: 1em 0px 0.5em; padding: 0px;"><span style="font-family: arial, sans-serif; font-size: medium;"><span style="line-height: 21px; font-size: 14px;"><span style="line-height: normal;">importorg.json.JSONArray;<br>importorg.json.JSONObject;<br><br>/**<br>*@authorTerryEmail:yaoxinghuoat126dotcom<br>*@versioncreate:Aug5,200911:17:23AM<br>*@versionupdate:Oct15,200900:11:00AM<br>*@description飞信API(fetionlib)HTTP简单调用举例另有Restlet的方式可供调用。网页版飞信首页:<a href="http://fetionlib.appspot.com/">http://fetionlib.appspot.com/</a><br>*<span style="color: #008000;">小提示:免费开通飞信:如果您的手机号没有开通飞信,可以到<a href="http://www.fetion.com.cn/">中国移动飞信网站</a>查看开通,或者直接编辑短信KTFX发送到10086开通</span><br>*<span style="color: #008000;">修改飞信密码:手机编辑新密码(6到16位,不能是中文或全角字符)内容发送到12520050</span><br>*<span style="color: #ff0000;">直接在浏览器里输入以下地址(您的手机号码和密码请自行更改,密码不要包含/,朋友号码请填写自己的手机号码):</span><br>*<a href="http://fetionlib.appspot.com/restlet/fetion/13812345678/password/13912345678/message" target="_blank">http://fetionlib.appspot.com/restlet/fetion/<strong>13812345678</strong>/<strong>password</strong>/<strong>13912345678</strong>/<strong>message</strong></a><br>*<span style="color: #ff0000;">以上四个用/隔开的加粗的地方,应该分别替换成:您的手机号、密码、对方手机号(可以写自己的手机号发给自己)、短信内容(不超过180字),</span><br>*<span style="color: #ff0000;">如果是密码错误,没有开通飞信,对方不是您好友等原因不能发送都是返回提示Message Not Sent,只有成功返回OK</span><br>*<span style="color: #ff0000;">如果要发送中文,最好用URLEncode(UTF-8编码,如“你好”Encode后为%E4%BD%A0%E5%A5%BD,现已支持)或后面举的例子(POST方式,注意调用的URL略有不同)</span><br>*<span style="color: #ff0000;">如果您可以收到自己发给自己的短信,恭喜您,测试通过,你可以用您熟悉的语言通过POST或GET调用,调用格式请看下面<span class="hilite2">Java</span>例子,其他语言类似</span><br>*如有疑问或对API的接口调用方式有任何更好的建议,<a href="http://spreadsheets.google.com/viewform?key=0AkW7RjU4e3Q3cF9XQWJkZTQzOHg1dnpGSmppYVo4ZkE&hl=en&cfg=true" target="_blank">欢迎提出宝贵意见</a><br>*<br>*<strong>现已经更新支持取得好友列表、POST方式的群发(8个或8个以下好友)和定时发送群发(定时群发最多30个好友),请看更新的例子</strong><br>*<br>*<sup style="color: red;">更新</sup><span style="font-weight: bold;">近期发现有人利用本程序给他人发送轰炸短信,给他人造成严重骚扰,同时也大量消耗本站资源,已作如下限制:</span><br>*<span style="font-weight: bold;">同一个手机号给同一个好友的发短信API以及其他的API(如:添加好友、获取好友列表等)请求间隔为30秒,30秒内的类似请求将无法完成。</span><br>*<span style="font-weight: bold;">注:考虑到实际需要,给自己发送短信(手机号和对方好友号码相同或者群发好友里面包含自己手机号)的API请求将不会有30秒时间间隔的限制!</span><br>*<br>*<strong><em>本飞信API接口程序由Google强力驱动、免费托管,将长期保留,示例程序用到的json包,请到www.json.org下载jar包,也可到<a href="http://cid-8b61c3bbf29e8e7a.skydrive.live.com/browse.aspx/lib/fetionlib" target="_blank">这里</a>下载</em></strong><br>*/<br>publicclassTest{<br>privatestaticLoglog=LogFactory.getLog(Test.class);<br><br>publicstaticvoidmain(String[]args){<br>//测试发短信<br>booleanb=fetchToSendSMS("13812345678","12345678",newString[]{"13812345678"},<br>"TestMessage");<br>System.out.println("SendMessageresult:"+b);<br><br>//测试取得好友列表<br>//JSONArrayfriends=fetchToGetFriends("13812345678","12345678");<br>//System.out.println("friends:/r/n"+(friends==null?"null":friends.toString()));<br><br>//测试添加好友<br>//intresult=fetchToAddFriend("13812345678","12345678","13812345678","TestMyName","TestFriendName");<br>//System.out.println("AddFriendresult:"+result);<br><br> //测试发送定时短信<strong>(注意是太平洋时间,所以2009-10-0901:00 是北京时间09:00发奥)</strong><br>//Stringsid=fetchToSendScheduleMsg("13812345678","12345678",newString[]{"13912345678"},"TestScheduleMessage","2009-10-0901:00");<br>//System.out.println("sid:"+sid);<br><br> //测试删除定时短信<br>//booleanb2=fetchToDeleteScheduleMsg("13812345678","12345678","123456");<br>//System.out.println("schedule message delete result:"+b2);<br>}<br><br>privatestaticfinalintTRY_TIMES=3;<br>privatestaticfinalintTIME_OUT=30000;<br><br>/**<br>*发送短消息更简单的Get方式(不支持群发,如要群发用下面POST方式,已更新),直接在浏览器里输入以下地址,手机号码和密码请自行改掉:<br>*<a href="http://fetionlib.appspot.com/restlet/fetion/13812345678/password/13912345678/message" target="_blank">http://fetionlib.appspot.com/restlet/fetion/13812345678/password/13912345678/message</a>成功返回OK<br>*否则返回MessageNotSent,如果要群发或者您的密码包含/或者需要提交中文消息避免可能的乱码最好请用以下的程序(POST方式)<br>*<strong>注意参数String[] friends 中的数组可以是好友的手机号,也可以是后面用程序取到的好友的uri,<span style="font-weight: normal;"><strong>详见后面取得好友列表的说明</strong></span><br>*<span style="font-weight: normal;"><strong>如fetchToSendSMS("13812345678","password",newString[]{"sip:12345678@fetion.com.cn;p=5065","13916416465","tel:15912345678"},"Test");</strong></span><br>*好友数不能超过8个,如果有需要,请用程序分开来多次调用</strong></span></span></span></p>
<p style="margin: 1em 0px 0.5em; padding: 0px;"><span style="font-family: arial, sans-serif; font-size: medium;"><span style="line-height: 21px; font-size: 14px;"><span style="line-height: normal;"><strong></strong> *<strong>注意:相同手机号,相同好友的请求的调用间隔要超过30秒,否则不成功(responseCode:406),但接受好友中包含你自己的手机号的请求不受30秒的限制!</strong><br>*/<br>publicstaticbooleanfetchToSendSMS(Stringmobile,Stringpassword,<br>String[]friends,Stringmessage){<br>// 加上UUID的目的是防止这样的情况,在服务器上已经成功发送短信,却在返回结果过程中遇到错误,<br>// 而导致客户端继续尝试请求,此时让服务器根据UUID分辨出该请求已经发送过,避免再次发送短信。<br>Stringuuid=UUID.randomUUID().toString();<br>for(inti=0;i<TRY_TIMES;i++){<br> intresponseCode=0;<br>try{<br>URLpostUrl=newURL(<br>"http://fetionlib.appspot.com/restlet/fetion");<br>HttpURLConnectionconnection=(HttpURLConnection)postUrl<br>.openConnection();<br>connection.setConnectTimeout(TIME_OUT);<br>connection.setReadTimeout(TIME_OUT);<br>connection.setDoOutput(true);<br>connection.setRequestMethod("POST");<br>connection.setUseCaches(false);<br>connection.setInstanceFollowRedirects(true);<br>connection.setRequestProperty("Content-Type",<br>"application/x-www-form-urlencoded");<br>connection.connect();<br>DataOutputStreamout=newDataOutputStream(connection<br>.getOutputStream());<br>Stringcontent="mobile="+mobile+"&uuid="+uuid<br>+"&password="+password+"&friend="+convertArrayToJSONString(friends)<br>+"&message="+URLEncoder.encode(message,"utf-8");<br>out.writeBytes(content);<br><br>out.flush();<br>out.close();<br><br>responseCode=connection.getResponseCode();<br>connection.disconnect();<br>if(responseCode==202)<br>returntrue;<br>else<br>returnfalse;<br>}catch(Exceptione){<br>log.warn("errorfetchToSendSMS,exception:"+e.getMessage()<br>+".tried"+i+"times");<br>}<br>}<br>returnfalse;<br>}</span></span></span></p>
<p></p>
<p><span style="font-family: arial, sans-serif; font-size: 14px;">//把数组转化成JSONString<br>private static String convertArrayToJSONString(String[] arr) throws Exception {<br> JSONArray ja = new JSONArray();<br> for (String a : arr)<br> ja.put(a);//ja.add(a);//?<br> return URLEncoder.encode(ja.toString(), "UTF-8");<br> }</span></p>
<p style="margin: 1em 0px 0.5em; padding: 0px;"><span><span style="line-height: 21px; font-family: arial, sans-serif; font-size: 14px;"><span><span style="text-align: left; line-height: 21px; font-family: arial, sans-serif; font-size: 14px;">}</span></span></span></span></p>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值