摘用的一个实现在线发送短信接口功能

 public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        //POST方法
        public string GetPage(string posturl,string postData)
        {
                Stream outstream = null;
                Stream instream = null;
                StreamReader sr = null;
                HttpWebResponse response = null;
                HttpWebRequest request = null;
                Encoding encoding = System.Text.Encoding.GetEncoding("gb2312");
                byte[] data = encoding.GetBytes(postData);
                // 准备请求...
                try
                {
                    // 设置参数
                    request = WebRequest.Create(posturl) as HttpWebRequest;
                    CookieContainer cookieContainer = new CookieContainer();
                    request.CookieContainer = cookieContainer;
                    request.AllowAutoRedirect = true;
                    request.Method = "POST";
                    request.ContentType = "application/x-www-form-urlencoded";
                    request.ContentLength = data.Length;
                    outstream = request.GetRequestStream();
                    outstream.Write(data, 0, data.Length);
                    outstream.Close();
                    //发送请求并获取相应回应数据
                    response = request.GetResponse() as HttpWebResponse;
                    //直到request.GetResponse()程序才开始向目标网页发送Post请求
                    instream = response.GetResponseStream();
                    sr = new StreamReader(instream, encoding);
                    //返回结果网页(html)代码
                    string content = sr.ReadToEnd();
                    string err = string.Empty;
                    return content;
                }
                catch (Exception ex)
                {
                    string err = ex.Message;
                    return string.Empty;
                }
        }

        //发送信息
        private void btnSend_Click(object sender, EventArgs e)
        {
            String userName = txtName.Text;           //用户名
            String userPassword = txtPassword.Text;   //密码
            String userVIP = txtVIP.Text;             //代理商ID

            String mobile = txtMobile.Text;           //号码集
            String content = txtContent.Text;         //内容

            String url = "HTTP://58.61.153.245:12250/xcdeal.asp";
            String data = "submitsendmsg=submit&textacc=" + CodeToUni(userName) + "&textpsw=" + CodeToUni(userPassword) + "&textphone=" + mobile + "&textcontent=" + CodeToUni(content);

            String backInfo = GetPage(url, data);

            //MessageBox.Show(GB2Unicode("123456"));
            MessageBox.Show(backInfo);

            /*
            -1 没有找到基本账号
            -2 没有手机号码或手机号码序列格式不规范
            -3 现时基本账号处于休眠状态,暂时不能发送信息
            -4 对不起,移动公司规定晚上9点后禁止批量发送,若有特殊需要,必须提前申请。
            -5 基本账号已被限制
            -6 贵宾账号被限制
            -7 没有找到贵宾账号
            -8 余额不足
            -9 服务器传输故障
            */


        }

        //读取账号信息
        private void btnFee_Click(object sender, EventArgs e)
        {
            MessageBox.Show(SendDataByGET("http://58.61.153.245:12250"));
            /*
               OK/123%%0%%OK
               
               OK/   读取成功
               123   剩余条数
               0     可欠费条数
               OK    账号可用
               %%    参数间的分隔符
            */
        }

        //GB2312转换成unicode编码 
        public string CodeToUni(string code)
        {
            if (code == "") return "";                                   // 参数空直接返回空

            string thisStr, thisCode, newcode = "";
            for (int i = 0; i < code.Length; i++)
            {
                thisStr = code.Substring(i, 1);
                thisCode = ((int)Convert.ToChar(thisStr)).ToString("X");  // 转换成十六进制
                newcode += thisCode.PadLeft(4, '0');                      // 左边加0,补足4位
            }
            return newcode;
        }

        //关闭程序
        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        //查询余额等账号信息
        public string SendDataByGET(string Url)
        {
            String userName = txtName.Text;           //用户名
            String userPassword = txtPassword.Text;   //密码
            String userVIP = txtVIP.Text;             //代理商ID

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + "?type=queryfeea&entid=" + userVIP + "&useracc=" + userName + "&pwd=" + userPassword);

            //MessageBox.Show(Url + "?type=queryfeea&entid=" + userVIP + "&useracc" + userName + "&pwd=" + userPassword);

            request.Method = "GET";
            request.ContentType = "text/html;charset=gb2312";
            
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("gb2312"));
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();

            return retString;
        }

    }
View Code

 

posted @ 2014-01-21 15:14  蜜雪粮液  阅读(468)  评论(0编辑  收藏  举报