技术开发 频道

发送短信程序的最终结果


【IT168技术文档】

  把数据库的知识弄了一下,发送应该没有太大的问题了.现在只需要把接受的程序弄出来就行了

  从数据库里提取一条数据,然后在更新一条数据.最后进行相关的数据库的修改,发出去.具体的格式都是给好的.接口都是已经弄好的.原程序如下
using System; using System.Data ; using System.Data .SqlClient ; using System.Collections; using System.ComponentModel; using System.Net ; using System.IO ; using System.Text ; using System.Text.RegularExpressions; using System.Web ; using System.Timers; namespace ConsoleApplication7 { /**//// <summary> /// Class1 的摘要说明。 /// </summary> class Class1 { /**//// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main(string[] args) { // Create a new Timer with Interval set to 10 seconds. System.Timers.Timer aTimer = new System.Timers.Timer(3000); aTimer.Elapsed+=new ElapsedEventHandler(OnTimedEvent); // Only raise the event the first time Interval elapses. aTimer.AutoReset = true; aTimer.Enabled = true; //Console.WriteLine("Press \'q\' to quit the sample."); while(Console.Read()!='q'); } private static void OnTimedEvent(object source, ElapsedEventArgs e) { //COMMANDID=05&CORPID=****&CPPW=******&SOURCEADDRFLAG=1&PHONE=******&SENDTIME=2006-10-20 17:25:00&TITLE=sujxt&CONTENT=wodeshijie string content=""; string time; time =DateTime.Now .ToString (); string mySelectQuery = "SELECT top 1 短信号码,短信内容 FROM 下行_短信 where 发送状态=2";    SqlConnection myConnection = new SqlConnection("server=localhost;database=下行_短信;uid=sa;pwd=sa"); SqlCommand myCommand = new SqlCommand(mySelectQuery,myConnection); myConnection.Open(); //更改数据把状态改成由2改成1 SqlCommand com=new SqlCommand ("update 下行_短信 set 发送状态=1 where 短信ID=(select top 1 短信ID from 下行_短信 where 发送状态=2)",myConnection); //加上这句看看 com.ExecuteNonQuery(); SqlDataReader myReader; myReader = myCommand.ExecuteReader(); // Always call Read before accessing data. while (myReader.Read()) { content="COMMANDID=05&CORPID=****8&CPPW=*****&SOURCEADDRFLAG=1&PHONE="+myReader.GetString (0)+"&SENDTIME="+time+"&TITLE=sujxt&CONTENT="+myReader.GetString (1); Console.WriteLine(content); } // always call Close when done reading. myReader.Close(); // Close the connection when done with it. myConnection.Close(); Class1 po=new Class1 (); string url="http://61.156.3.58/SendSms800"; //long SMSID=40065310137000000001; //string content="COMMANDID=05&CORPID=********&CPPW=*******&SOURCEADDRFLAG=1&PHONE=13969191541&SENDTIME=2006-10-20 17:25:00&TITLE=sujxt&CONTENT=wodeshijie"; po.postget (url,content); // TODO: 在此处添加代码以启动应用程序 // } public void postget(string url, string content) { try { Encoding encoding = Encoding.GetEncoding("GB2312"); byte[] data = encoding.GetBytes(content); // 准备请求 HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url); myRequest.Method = "POST"; myRequest.ContentType="application/x-www-form-urlencoded"; myRequest.ContentLength = data.Length; Stream newStream=myRequest.GetRequestStream(); // 发送数据 newStream.Write(data,0,data.Length); newStream.Close(); //接收数据 HttpWebResponse webResponse=(HttpWebResponse)myRequest.GetResponse(); //Console.WriteLine ("aa"); Stream stream=webResponse.GetResponseStream(); System.IO.StreamReader streamReader = new StreamReader(stream, System.Text.Encoding.GetEncoding("GB2312")); string content1 = streamReader.ReadToEnd(); // 关闭相关对象 Console.WriteLine (content1); streamReader.Close(); webResponse.Close(); } catch (Exception e) { Console.WriteLine ("error:{0}"+e.ToString ()); } } } }
0
相关文章