zoukankan      html  css  js  c++  java
  • 发送短信程序

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

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

  • 相关阅读:
    Shell入门教程:命令替换 $() 和 ``
    CentOS启用sudo,禁用root远程登录
    .htaccess 基础教程(四)Apache RewriteCond 规则参数
    .htaccess 基础教程(三)RewriteCond标志符,RewriteRule适用的标志符
    .htaccess 基础教程(二)
    .htaccess 基础教程(一)
    phpMyAdmin 个性化设置,字体大小设置,去掉“以树形显示数据库”,禁用“发送错误报告”
    PHP的$_SERVER['PHP_SELF']造成的XSS漏洞攻击及其解决方案
    PHP变量作用域(花括号、global、闭包)
    获取PHP文件绝对地址$_SERVER['SCRIPT_FILENAME'] 与 __FILE__ 的区别
  • 原文地址:https://www.cnblogs.com/zlfucku/p/1703680.html
Copyright © 2011-2022 走看看