傳送簡訊程式
來源:互聯網
上載者:User
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data;using System.Data.SqlClient;using System.Configuration;namespace SendReady{ class Program { static void Main(string[] args) { Console.Title = "發送預約提醒簡訊"; System.Threading.Thread th = new System.Threading.Thread(new System.Threading.ThreadStart(SendAllTime)); th.IsBackground = true; th.Start(); Console.ReadLine(); } public static string UrlEncode(string url) { byte[] bs = Encoding.GetEncoding("GB2312").GetBytes(url); StringBuilder sb = new StringBuilder(); for (int i = 0; i < bs.Length; i++) { if (bs[i] < 128) sb.Append((char)bs[i]); else { sb.Append("%" + bs[i++].ToString("x").PadLeft(2, '0')); sb.Append("%" + bs[i].ToString("x").PadLeft(2, '0')); } } return sb.ToString(); } public static void SendAllTime() { while (true) { try { DataSet ds; using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SendMTMessage.Properties.Settings.eztcnConnectionString"].ConnectionString)) { conn.Open(); SqlDataAdapter adapter = new SqlDataAdapter("select * from tbReadySendSms where datediff(hh, fSendTime, getdate()) = 1 and fstate = '002'", conn); ds = new DataSet(); adapter.Fill(ds); adapter.Dispose(); conn.Close(); conn.Dispose(); } foreach (DataRow item in ds.Tables[0].Rows) { //string msgId = bllSms.GetMaxId(); Random rand = new Random(); string url = "http://www.sms10000.com.cn/szdx_sdk/SMS?cmd=send&uid=****38368&psw=*****&mobiles=" + item["fmobile"] + "&msgid=" + rand.Next(999999).ToString() + "&msg=" + item["fcontent"] + ""; string encodeURL = UrlEncode(url); System.Net.WebRequest sendHttp = System.Net.WebRequest.Create(encodeURL); System.Net.WebResponse retResponse = sendHttp.GetResponse(); System.IO.Stream stream = retResponse.GetResponseStream(); System.IO.StreamReader readerResponse = new System.IO.StreamReader(stream, System.Text.Encoding.Default); string result = readerResponse.ReadToEnd(); if (result != "100") { throw new Exception("發送失敗!"); } else { using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SendMTMessage.Properties.Settings.eztcnConnectionString"].ConnectionString)) { conn.Open(); using (SqlTransaction trans = conn.BeginTransaction()) { try { using (SqlCommand cmd = new SqlCommand("", conn, trans)) { cmd.CommandType = CommandType.Text; cmd.CommandText = "update tbReadySendSms set fstate = '001' where fReadySendSmsId = " + item["fReadySendSmsId"]; cmd.ExecuteNonQuery(); } trans.Commit(); } catch { trans.Rollback(); } } conn.Close(); } } retResponse.Close(); readerResponse.Close(); stream.Close(); } ds.Dispose(); System.Threading.Thread.Sleep(100); } catch (Exception ex) { } } } }}