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号

手机站二维码