在JAVA中通過簡訊的形式發送到手機號碼上

來源:互聯網
上載者:User

標籤:手機號碼   package   import   java   簡訊   

package com.fetion.test;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

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;

/**
 * API2.1調用:f.php?phone=xxxxxx&pwd=xxx&to=xxxx&msg=xxxx&type=0
 *以上介面參數詳細說明 VIP API
 *1.phone:手機號
 *2.pwd:飛信密碼
 *3.to:發送給誰(手機號或飛訊號)
 *4.msg:飛信內容
 *5.type:操作 0(空)傳送簡訊 1檢查好友 2添加好友
 *6.u:備用參數:當發送內容為亂碼時 在最後加上&u=1
 */
public class Fetion {
    //自己的手機號
    private static String PHONE = "";  
    //自己的飛信密碼
    private static String PWD = "";  
    //對方的手機號
    private static String TO = "";  
    //
    private static String configuration;
   
    private Properties config = new Properties();
   
    public Fetion(){
        init();
    }
    //初始化設定檔
    public void init(){
        try {
            InputStream is = new FileInputStream("FetionConfig");
            config.load(new InputStreamReader(is));
            configuration=config.getProperty("WeekendGreetings");
            System.out.println(configuration);
            PHONE=config.getProperty("phone");
            PWD=config.getProperty("pwd");
            TO=config.getProperty("to");
            is.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
     public static void sendMsg(String _phone,String _pwd,String _to,String _msg) throws HttpException, IOException{  
                 HttpClient client = new HttpClient();  
                 PostMethod post = new PostMethod("http://3.ibtf.sinaapp.com/f.php");  
                 post.addRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");//在標頭檔中設定轉碼   
                 NameValuePair[] data ={   
                         new NameValuePair("phone", _phone),  
                         new NameValuePair("pwd", _pwd),  
                         new NameValuePair("to",_to),  
                         new NameValuePair("msg",_msg),  
                         new NameValuePair("type","0")  
                         };  
                 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("utf-8"));   
                 //System.out.println(result);   
                 System.out.println("ok!");  
                 post.releaseConnection();  
             }  
     public  void start(){
         try {
            FileReader fr = new FileReader(configuration);
            BufferedReader br = new BufferedReader(fr);
            String MSG=null;
            
            while ((MSG=br.readLine())!=null) {
                Fetion.sendMsg(PHONE, PWD, TO, MSG);  
            }
            br.close();
            fr.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        
     }
     public static void main(String[] args) {  
         Fetion f = new Fetion();
         f.start();
        }  

}

在JAVA中通過簡訊的形式發送到手機號碼上

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.