微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

调用WebServices接口实现短信收发

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SMS.cn.woxp.gateway;
using System.IO;
using System.Xml;
namespace SMS
{
    /*采用web Service接口实现短信收发
     * 本程序适合网关版本v1.0开发
     本演示代码由星幻科技提供.如有其它问题请与我们联系.
     * 如果发送失败原因与通道有关.可能是由于某些原因导致通道关闭.请切换其它通道使用
     * 短信通道发送出现延时比较长.建议切换通道发送.[任何短信网关都会出现短信延时.请谅解]
     * -----------------------------------------------------------------
     * 本演示代码由[星幻科技 www.woxp.cn]提供,最新接口请登陆会员中心查看
     * -----------------------------------------------------------------
     */
    public partial class Form1 : Form
    {
        private cn.woxp.gateway.WebSMS wsms = new SMS.cn.woxp.gateway.WebSMS();
        private string strIdentity = "";
        public Form1()
        {
            InitializeComponent();
        }

        private void label7_Click(object sender,EventArgs e)
        {

        }

        private void Form1_Load(object sender,EventArgs e)
        {          
            this.l_money.Text = "0.00";
        }
        private bool CheckGate()
        {
            int temp = 0;
            if (this.t_eid.Text.ToString().Trim() == "" || this.t_gateid.Text.ToString().Trim() == "" || this.t_pwd.Text.ToString().Trim() == "" || this.t_uid.Text.ToString().Trim() == "")
            {
                MessageBox.Show("网关参数输入不全!请重新输入","网关参数配置",MessageBoxButtons.OK,MessageBoxIcon.Error);
                return false;
            }
            if (!Int32.TryParse(this.t_eid.Text.ToString().Trim(),out temp) || !Int32.TryParse(this.t_gateid.Text.ToString().Trim(),out temp))
            {
                MessageBox.Show("企业代码或者网关通道ID必须为数字!请重新输入.",MessageBoxIcon.Error);
                return false;
            }
            return true;
        }
        private void button1_Click(object sender,EventArgs e)
        {
            this.button1.Enabled = false;
            if (!CheckGate())
            {
                this.button1.Enabled = true;
                return;
            }
            if (this.t_sendNo.Text.ToString().Trim() == "" || this.t_sendMemo.Text.ToString().Trim() == "")
            {
                MessageBox.Show("请输入目标号码和短信内容","发送短信",MessageBoxIcon.Error);
                this.t_sendNo.Focus();
                this.button1.Enabled = true;
                return;
            }
            if (this.t_sendTime.Text.ToString().Trim() != "")
            {
                DateTime dt;
                if (!DateTime.TryParse(this.t_sendTime.Text.ToString().Trim(),out dt))
                {
                    MessageBox.Show("定时发送格式不正确!",MessageBoxIcon.Error);
                    this.button1.Enabled = true;
                    this.t_sendTime.Focus();
                    return;
                }
                if (dt <= DateTime.Now)
                {
                    MessageBox.Show("定时发送时间必须大于当前时间!",MessageBoxIcon.Error);
                    this.button1.Enabled = true;
                    return;
                }
            }
            strIdentity= wsms.GetIdentityMark(Int32.Parse(this.t_eid.Text.ToString().Trim()),this.t_uid.Text.ToString().Trim(),this.t_pwd.Text.ToString().Trim(),Int32.Parse(this.t_gateid.Text.ToString().Trim()));
            if(strIdentity == null || strIdentity =="")
            {
                MessageBox.Show("获取身份标识串失败!",MessageBoxIcon.Error);
                this.button1.Enabled = true;
                return;
            }
            string js="";
            //全速提交短信
            SendResult status = wsms.FastSend(strIdentity,this.t_sendNo.Text.ToString().Trim(),this.t_sendMemo.Text.ToString().Trim(),this.t_sendTime.Text.ToString().Trim(),"");
            if(status.RetCode>0)
            {
                this.l_money.Text = wsms.GetMoney(strIdentity).ToString("0.00");
                js="发送成功!共发送:"+status.RetCode.ToString()+"条";
                MessageBox.Show(js,"发送短信状态",MessageBoxIcon.@R_403_4045@ion);
            }
            else
            {
                js="发送失败,代码:"+status.RetCode.ToString().Trim()+",原因:"+status.ErrorDesc;
                MessageBox.Show(js,MessageBoxIcon.Error);
            }
           
            this.button1.Enabled = true;
        }

        /// <summary>
        /// 收到的XML转成dataset型
        /// </summary>
        /// <param name="xmlData"></param>
        /// <returns></returns>
        public DataSet ConvertXMLToDataSet(string xmlData)
        {
            StringReader stream = null;
            XmlTextReader reader = null;
            try
            {
                DataSet xmlDS = new DataSet();
                stream = new StringReader(xmlData);
                reader = new XmlTextReader(stream);
                xmlDS.readxml(reader);
                return xmlDS;
            }
            catch (Exception ex)
            {
                string strTest = ex.Message;
                return null;
            }
            finally
            {
                if (reader != null)
                    reader.Close();
            }
        }

        private void button2_Click(object sender,EventArgs e)
        {
            this.button2.Enabled = false;
            if (!CheckGate())
            {
                this.button2.Enabled = true;
                return;
            }
            strIdentity = wsms.GetIdentityMark(Int32.Parse(this.t_eid.Text.ToString().Trim()),Int32.Parse(this.t_gateid.Text.ToString().Trim()));
            if (strIdentity == null || strIdentity == "")
            {
                MessageBox.Show("获取身份标识串失败!",MessageBoxIcon.Error);
                this.button1.Enabled = true;
                return;
            }
            string xml = wsms.readxml(strIdentity);          
            if (xml == null || xml.ToString().Trim()=="")
            {
                MessageBox.Show("没有收到回复短信!","接收短信",MessageBoxIcon.@R_403_4045@ion);
                this.button2.Enabled = true;
                return;
            }
            DataSet ds = new DataSet();          
            try
            {
                int code = 0;
                if (Int32.TryParse(xml,out code))
                {
                    MessageBox.Show("加载短信失败,原因:" +wsms.GetErrorHint(code),MessageBoxIcon.@R_403_4045@ion);
                    this.button2.Enabled = true;
                    return;
                }
                ds = ConvertXMLToDataSet(xml);
            }
            catch(Exception ex)
            {
                MessageBox.Show("加载短信异常,原因:"+ex.Message,MessageBoxIcon.@R_403_4045@ion);
                this.button2.Enabled = true;
                return;
            }
            this.dgv_receive.DataSource = ds.Tables[0];
            this.button2.Enabled = true;
        }

        private void button3_Click(object sender,EventArgs e)        {            this.t_sendMemo.Text = "";            this.t_sendNo.Text = "";            this.t_sendTime.Text = "";            this.button1.Enabled = true;            this.button2.Enabled = true;            this.button3.Enabled = true;        }    }}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐