⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 短信接口及demo程序本接口专为中小型网站提供。开通简单,平台稳定
💻 CS
字号:
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 www.xhsms.com.
     * 如果发送失败原因与通道有关.可能是由于某些原因导致通道关闭.请切换其它通道使用
     * 短信通道发送出现延时比较长.建议切换通道发送.[任何短信网关都会出现短信延时.请谅解]
     * 中国星幻科技有限公司 www.xhsms.com 星幻短信平台 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必须为数字!请重新输入.", "网关参数配置", MessageBoxButtons.OK, 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("请输入目标号码和短信内容", "发送短信", MessageBoxButtons.OK, 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("定时发送格式不正确!", "发送短信", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    this.button1.Enabled = true;
                    this.t_sendTime.Focus();
                    return;
                }
                if (dt <= DateTime.Now)
                {
                    MessageBox.Show("定时发送时间必须大于当前时间!", "发送短信", MessageBoxButtons.OK, 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("获取身份标识串失败!", "发送短信", MessageBoxButtons.OK, 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, "发送短信状态", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                js="发送失败,代码:"+status.RetCode.ToString().Trim()+",原因:"+status.ErrorDesc;
                MessageBox.Show(js, "发送短信状态", MessageBoxButtons.OK, 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()), 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("获取身份标识串失败!", "发送短信", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.button1.Enabled = true;
                return;
            }
            string xml = wsms.ReadXml(strIdentity);           
            if (xml == null || xml.ToString().Trim()=="")
            {
                MessageBox.Show("没有收到回复短信!", "接收短信", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.button2.Enabled = true;
                return;
            }
            DataSet ds = new DataSet();           
            try
            {
                int code = 0;
                if (Int32.TryParse(xml, out code))
                {
                    MessageBox.Show("加载短信失败,原因:" +wsms.GetErrorHint(code), "接收短信", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.button2.Enabled = true;
                    return;
                }
                ds = ConvertXMLToDataSet(xml);
            }
            catch(Exception ex)
            {
                MessageBox.Show("加载短信异常,原因:"+ex.Message, "接收短信", MessageBoxButtons.OK, MessageBoxIcon.Information);
                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;
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -