JAVA利用飞信接口发送短信

飞信接口地址: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

你可能感兴趣的:(JAVA利用飞信接口发送短信)