摘用的一個實現線上傳送簡訊介面功能

weixin_34321977發表於2014-01-21
 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

 

相關文章