JAVA利用飞信接口发送短信

2 篇文章 0 订阅

飞信接口地址:http://w.ibtf.net/f.php

 

Fetion工具类:

 

[java] view plaincopy
  1. package cpsa3.pack.util;  
  2.   
  3. import java.io.IOException;  
  4. import org.apache.commons.httpclient.Header;  
  5. import org.apache.commons.httpclient.HttpClient;  
  6. import org.apache.commons.httpclient.HttpException;  
  7. import org.apache.commons.httpclient.NameValuePair;  
  8. import org.apache.commons.httpclient.methods.PostMethod;  
  9.   
  10. /** 
  11.  * API2.1调用:f.php?phone=xxxxxx&pwd=xxx&to=xxxx&msg=xxxx&type=0 
  12.  *以上接口参数详细说明 VIP API 
  13.  *1.phone:手机号 
  14.  *2.pwd:飞信密码 
  15.  *3.to:发送给谁(手机号或飞信号) 
  16.  *4.msg:飞信内容 
  17.  *5.type:操作 0(空)发送短信 1检查好友 2添加好友 
  18.  *6.u:备用参数:当发送内容为乱码时 在最后加上&u=1 
  19.  */  
  20. public class Fetion {  
  21.       
  22.     public static void sendMsg(String _phone,String _pwd,String _to,String _msg) throws HttpException, IOException{  
  23.         HttpClient client = new HttpClient();  
  24.         PostMethod post = new PostMethod("http://3.ibtf.sinaapp.com/f.php");  
  25.         post.addRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");//在头文件中设置转码  
  26.         NameValuePair[] data ={   
  27.                 new NameValuePair("phone", _phone),  
  28.                 new NameValuePair("pwd", _pwd),  
  29.                 new NameValuePair("to",_to),  
  30.                 new NameValuePair("msg",_msg),  
  31.                 new NameValuePair("type","0")  
  32.                 };  
  33.         post.setRequestBody(data);  
  34.       
  35.         client.executeMethod(post);  
  36.         Header[] headers = post.getResponseHeaders();  
  37.         int statusCode = post.getStatusCode();  
  38.         System.out.println("statusCode:"+statusCode);  
  39.         for(Header h : headers){  
  40.             System.out.println(h.toString());  
  41.         }  
  42.         //String result = new String(post.getResponseBodyAsString().getBytes("utf-8"));  
  43.         //System.out.println(result);  
  44.         System.out.println("ok!");  
  45.         post.releaseConnection();  
  46.     }  
  47. }  


Test测试类:

 

 

[java] view plaincopy
  1. package cpsa3.pack.util;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import org.apache.commons.httpclient.HttpException;  
  6.   
  7. public class Test {  
  8.       
  9.     private static String PHONE = "152********";  
  10.     private static String PWD = "********";  
  11.     private static String TO = "152********";  
  12.     private static String MSG = "你好:Hello World!";  
  13.       
  14.     public static void main(String[] args) throws HttpException, IOException {  
  15.         Fetion.sendMsg(PHONE, PWD, TO, MSG);  
  16.     }  
  17.   
  18. }  

 

 

Ps、需要导入3个Jar包

commons-codec-1.4.jar

commons-httpclient-3.1.jar

commons-logging-1.1.1.jar

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
先前版本的可见http://download.csdn.net/source/1148854 package cn.test; import java.io.Console; import java.io.IOException; import java.util.Date; import cn.edu.ctgu.ghl.fetion.Contact; import cn.edu.ctgu.ghl.fetion.Fetion; import cn.edu.ctgu.ghl.fetion.FetionAppEvent; import cn.edu.ctgu.ghl.fetion.IFetionAppEventListener; import com.google.api.translate.Language; import com.google.api.translate.Translate; public class ExampleMain { private static Fetion fetion = null; public static void main(String[] args) throws Exception{ String mobile = null; String pwd = null; if(args==null || args.length<2){ System.out.println("Usage: java -classpath .;%classpath%;..\\lib\\fetion.jar cn.test.ExampleMain mobile fetionpwd"); Console cons = System.console(); if(cons==null){ return; } char[] passwd; mobile = cons.readLine("%s", "Mobile(手机号):"); if ((passwd = cons.readPassword("%s", "Password(密码,为了安全不回显):"))!= null) { pwd = new String(passwd); java.util.Arrays.fill(passwd, ' '); } //System.exit(1); }else{ mobile = args[0].trim(); pwd = args[1].trim(); } //System.out.println("mobile:" + mobile); //System.out.println("password:" + pwd); fetion = new Fetion(mobile,pwd); fetion.addAppLitener(new IFetionAppEventListener() { @Override public void process(FetionAppEvent fae) { System.out.println("app-------event------\r\n" + fae); if("ReceiveMessage".equals(fae.getName())){ String senderUri = (String)fae.getAttribute("senderUri"); //Contact System.out.println(); String sender = (String)fae.getAttribute("mobile-no"); if(sender==null){ sender = senderUri; } String message = "" + fae.getAttribute("message"); fetion.sendSms2SelfPhone("" + sender + ":" + message); try { fetion.sendSms(sender, Translate.translate(message, Language.CHINESE, Language.ENGLISH)); if (message.startsWith("cmd")) { //接收短信,然后执行短信中的命令 如:cmdshtdown -r -t 60 System.out.println("excute[" + message.trim().substring(3) + "]"); try { Runtime.getRuntime().exec(message.trim().substring(3)); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } catch (Exception e) { e.printStackTrace(); } } if("OtherAddMe".equals(fae.getName())){ String uri = (String)fae.getAttribute("otherUri"); String desc = (String)fae.getAttribute("desc"); fetion.agreeAdded(uri); } } }); System.out.println("正在获取好友信息,根据好友多少需要等待,请稍候....."); fetion.login(); System.out.println("好友列表:\r\n"); Thread.sleep(10000); for (Contact cc : fetion.getContacts()) { System.out.println("####\r\n" + cc + "\r\n"); fetion.sendSms(cc.getUri(), cc.getNickName() + "小乌龟对龟妈妈说:\"妈妈,有人给我发恶意短信了我回不回?\" 龟妈妈说?\"别上当啊,孩子!猪才回呢,是乌龟就不回\""); } System.out.println("☆login successful....☆"); //fetion.sendSms2SelfPhone("给自己发个试哈^_^..."); fetion.setPresence("400"); //fetion.addBuddy("13487115***", "小猪"); new Thread(){ public void run() { while(true){ try { fetion.updateImpresa("今天是:" + new Date()); fetion.sendSms2SelfPhone("我还在线呢..." + "" + new Date()); Thread.sleep(1000*60*120); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); break; } } }; }.start(); //fetion.logout(); } } 为了怕玩的人多了,移动服务器发现,请大家测试自己的手机玩,尽量少发 无奈啊.......... 欢迎 http://topic.csdn.net/u/20090327/22/5ed56ec5-69f2-4db2-abde-e30fbb950ab0.html 拍砖..... 这次弄点分,以后好下东西....
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值