Let the storm money come!

发送短信程序

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

posted @   精密~顽石  阅读(449)  评论(1编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
< 2010年4月 >
28 29 30 31 1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 1
2 3 4 5 6 7 8
在通往地狱的路上,加班能使你更快到达。
点击右上角即可分享
微信分享提示