当前位置:编程学习 > C#/ASP.NET >>

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. }
补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,