程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#編寫移動與聯通接口

C#編寫移動與聯通接口

編輯:C#入門知識

  1. //頁面調用
  2.  private void SendMassage(string Telephone, string text)
  3.     {
  4.         SendMobile sm = new SendMobile(Telephone, text);
  5.         sm.SendMsg();
  6. }
  7. //引用
  8. using System.Text;
  9. using System.Net;
  10. using System.IO;
  11. //using cn.sh.unicom.groupsms;
  12. /// <summary>
  13. /// SendMobile 的摘要說明
  14. /// </summary>
  15. public class SendMobile
  16. {
  17.     public SendMobile()
  18.     {
  19.         //
  20.         // TODO: 在此處添加構造函數邏輯
  21.         //
  22.     }
  23.     private string _MobNum;
  24.     private string _MobMsg;
  25.     public SendMobile(string MobNum, string MobMsg)
  26.     {
  27.         this._MobNum = MobNum;
  28.         this._MobMsg = MobMsg;
  29.     }
  30.     public void SendMsg()
  31.     {
  32.         if (CheckType(this._MobNum))
  33.             ToMobile();
  34.         else
  35.             ToUnicom();
  36.     }
  37.     public void ToMobile()
  38.     {//移動的接口
  39.         Encoding encoding = Encoding.GetEncoding("gb2312");
  40.         string enterpriseid = "123456";//企業代碼
  41.         string accountid = "15811150014";//帳號
  42.         string pwd = "630420";//密碼
  43.         string postData = "enterpriseid=" enterpriseid "&accountid=" accountid "&pswd=" pwd "&mobs=" this._MobNum "&msg=" this._MobMsg;
  44.         string strUrl = "http://211.136.163.68:8000/httpserver";
  45.         byte[] data = encoding.GetBytes(postData);
  46.         // 准備請求...
  47.         HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(strUrl);
  48.         myRequest.Method = "POST";
  49.         myRequest.ContentType = "application/x-www-form-urlencoded";
  50.         myRequest.ContentLength = data.Length;
  51.         Stream newStream = myRequest.GetRequestStream();
  52.         // 發送數據
  53.         newStream.Write(data, 0, data.Length);
  54.         newStream.Close();
  55.     }
  56.     public void ToUnicom()
  57.     {//聯通的接口
  58.         cn.sh.unicom.groupsms.Login myLogin = new cn.sh.unicom.groupsms.Login();
  59.         string MySessionID = "";
  60.         myLogin.username = "yourname"; //用戶名123456789
  61.         myLogin.userType = "0";
  62.         myLogin.password = "yourpwd";  //密碼
  63.         LoginMes mes1 = new LoginMes();
  64.         mes1.login = myLogin;
  65.         SmWSImplService Binding = new SmWSImplService();
  66.         OpResult Value1 = new OpResult();
  67.         try
  68.         {
  69.             Value1 = Binding.memberLogin(mes1);
  70.         }
  71.         catch
  72.        {
  73.      
  74.         }
  75.         MySessionID = Value1.sessionId;//得到sessionid
  76.         SendBatchSMS(Binding, MySessionID, this._MobMsg, "companycode");//companycode為企業代號1112345
  77.         Value1 = Binding.logout(mes1);
  78.     }
  79.     public bool CheckType(string MobileNum)
  80.     {
  81.         string ForeStr = MobileNum.Substring(0, 3);
  82.         int ForeNum = Convert.ToInt32(ForeStr);
  83.         if (ForeNum <= 134)
  84.             return false;
  85.         else
  86.             return true;
  87.     }
  88. }

  1. 上一頁:
  2. 下一頁: