发送短信程序

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)
                {
                    
                }
            }
        }
    }
}

走向地狱的途中,不小心走了程序员这条路,路上一个个黑心的老板,和暗无天日的加班,我才发现,通往地狱的路径中,我们这行是最短的。

原文地址:https://www.cnblogs.com/zlfucku/p/1703680.html