亚洲今日精彩视频_精品一级黄色_免费一级A片在现观看视频_8050福利视频 - 一级免费黄色片

7*24小時應急電話:15927160396
首頁 新聞資訊 技術文章
C#異步調用飛信WebService實時接收短信

第一步:

      新創建一Win窗口項目,并添加Web引用飛信WebService,引用地址:http://io.139icq.com/fWebSer.asmx?WSDL,重命名引用為:FetionObj

第二步:

      添加以下相關控件到主窗口

1、加TextBox控件用于顯示收到的相關信息 命名:showtxt,設此控件Multiline=true,使能顯示多行,并拖大些。

2、加TextBox控件用于輸入登錄號碼 命名:loginNo。

3、加TextBox控件用于輸入登錄密碼 命名:loginPass。

4、加一Button控件命名Login,設置Text=登錄,Click事件指向Login_Click函數。

第三步:

      編寫相關代碼

1、在代碼最上面添加引用:

using FetionObj.fWebSer;

2、以下是異步調用的相關源碼:

        private string _myMNo=""; //保存登錄號

        private fWebSer fws=null; //聲名接口類

        private bool reAsync=false; //連續接收信息開關

        public Form1()
        {
            InitializeComponent();


            fws = new fWebSer(); //創建WebService接口類
            fws.CookieContainer = new System.Net.CookieContainer(); //這句很重要,否則將不能登錄
        }

        public bool Login_Click(object sender, EventArgs e)
        {
                _myMNo = loginNo.Text;

              
                fws.Login(_myMNo, loginPass.Text); //要先發送登錄命令
                if (!reAsync) //接收已經停止才能重新開始異步接收
                {
                    reAsync = true;
                    DelegateTreCom dn = new DelegateTreCom(TreCom); //實例化委托并初賦值
                    IAsyncResult iar = dn.BeginInvoke(_myMNo, new AsyncCallback(CallBackTreCom), dn); //異步開始執行
                }
        }

        //多線程安全委托輸出
        private delegate void MyControl(Control myc, string txt);
        private void Setout(Control myc, string txt)
        {
                if (myc.InvokeRequired)
                    myc.Invoke(new MyControl(Setout), myc, txt);
                else
                    myc.Text = txt + "\r\n" + myc.Text;
        }

        private delegate void DelegateTreCom(string recom); //定義委托
        private void TreCom(string myMNo)
        {//用異步處理收到的信息的函數
            try
            {
                string remsg=fws.ReMsg(myMNo, 40); //長連接方式接收數據,沒數據時最多可以等待40秒再返回
                string[] rearr = remsg.Split('\n');  //拆分事件記錄
                for (int i = 0; i < rearr.Length; i++)  //處理收到的所有事件
                {
                    //分析每一事件
                    string com = "", rfno = "", rmno = "", msg = "", errtxt = "", rgno = "", fname = "";
                    string[] comarr = rearr[i].Split('&');
                    for (int j = 0; j < comarr.Length; j++)
                    {
                        string[] varr = comarr[j].Split('=');
                        if (varr.Length < 2) continue;
                        if (varr[0] == "com") com = varr[1];
                        if (varr[0] == "rfno") rfno = varr[1];
                        if (varr[0] == "rmno") rmno = varr[1];
                        if (varr[0] == "msg") msg = varr[1];
                        if (varr[0] == "errtxt") errtxt = varr[1];
                        if (varr[0] == "rgno") rgno = varr[1];
                        if (varr[0] == "fname") fname = varr[1];
                    }
                    if (com == "rmsg")
                    {//收到新信息
                        Setout(showtxt,"收到"+ fname+"("+rfno+"|"+rmno+")新信息:"+msg);
                    }
                    if (com == "sendok")
                    {//發送成功

                        Setout(showtxt,"發送到"+ rfno+"成功,內容:"+msg);
                    }
                    if (com == "senderr")
                    {//發送失敗
                        Setout(showtxt,"發送到"+ rfno+"失敗,錯誤信息:"+errtxt);
                    }
                    if (com == "loginok")
                    {//登錄成功

                        Setout(showtxt,"登錄成功,自己的飛信號:"+ rfno+",手機號:"+rmno+",昵稱:"+fname);
                    }
                    if (com == "loginerr")
                    {//登錄失敗
                        reAsync = false;  //停止接收事件

                        Setout(showtxt,"登錄失敗");
                    }
                    if (com == "fadd")
                    {//被添加為好友

                        Setout(showtxt,"被添加為好友,對方信息:"+msg.Trim());
                    }
                    if (com == "list")
                    {//好友列表完成

                        Setout(showtxt,"好友列表完成");
                    }
                    if (com == "logout")
                    {//被注銷
                        reAsync = false;  //停止接收事件
                        Setout(showtxt,"已經注銷");
                    }
                    if (com == "changed")
                    {//好友狀態改變

                        string[] farr = msg.Trim().Split('|');
                        if (farr.Length >= 7)
                        {
                            if (farr[5] == "聊天")
                            {
                                Setout(showtxt,"好友打開聊天窗口:"+msg.Trim());
                            }
                            else
                            {
                                Setout(showtxt,"好友狀態改變:"+msg.Trim());
                            }
                        }
                    }
                }
            }
            catch (Exception er) {
                Setout(showtxt,"接收出錯:"+er.Message+er.StackTrace);
            }
            if (reAsync)
            {//重復調用接收數據
                Thread.Sleep(2000); //控制調用速度
                DelegateTreCom dn = new DelegateTreCom(TreCom); //實例化委托并初賦值
                IAsyncResult iar = dn.BeginInvoke(myMNo, new AsyncCallback(CallBackTreCom), dn); //異步開始執行
            }
        }
        //異步完成時,執行的方法(回調方法)

        private void CallBackTreCom(IAsyncResult ar)
        {
            //從異步狀態ar.AsyncState中,獲取委托對象
            DelegateTreCom dn = (DelegateTreCom)ar.AsyncState;
            //一定要EndInvoke,否則...
            dn.EndInvoke(ar);
        }

 其它象發送信息等調用就簡單了,大家自己添加控件實現吧

 

本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/mywwl/archive/2009/05/11/4169455.aspx

版權所有:武漢網福互聯科技有限公司    鄂ICP備09022096號
業務QQ:23444550 客服QQ:267052100 電郵:23444550@qq.com  

鄂公網安備 42010602000905號

手機站二維碼