首页 新闻 会员 周边 捐助

发送短信超速问题

0
悬赏园豆:100 [已解决问题] 解决于 2016-08-08 13:48
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;

using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading; 
using System.Net;
using System.IO;
using System.Security.Cryptography;
using System.Web.Script.Serialization;
using System.Runtime.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;

namespace Grape.JG.BLL
{
    public partial class Sendsms_Http_Form 
    {
        public string access_token = "";
        public Sendsms_Http_Form()
        {
           
        }

        public string Sendsms_Http_Form_Load(string phone,string nr)
        {
            //登录并发送
          return   LoginGo(phone, nr);//手机号,短信内容
            
            
        }
      
        public static string LoginGo(string phone, string nr)
        {
            //Http地址
            string ContentType = "application/x-www-form-urlencoded";//类型application/x-www-form-urlencoded
            //string Url = "http://112.33.1.13:80/app/http";//http地址
            string Url = "http://mas.ecloud.10086.cn/app/http";//正式http地址

          
            string Ecname = "xxx";//集团用户名
            string Username = "xxx";//sdk帐号
            string Password = "xxx";//sdk密码
            string LoginType = "/authorize?"; 

            //短信(普通)
            string mas_user_id = "24adfceb-7008-47b3-a133-8201f7cf52550";
            string mobiles =phone ;//电话号码
            string content = nr ;//"这是一条测试短信121313--";
            string sign = "Y5l81CYQ0";//签名编码
            string serial = "1065731010358";
            string SentType = "/sendSms?";
            string access_token = "NHh6LOX98qY81VS1dbF9FWU9jLlM9F4";
           
             string LoginStatus="";
            string T_StengStatus = "";
            try
            {
              
                 LoginStatus = PostHttp_Login(Url, LoginType, Ecname, Username, Password, ContentType);//登录验证
                  
             
                JObject json = JObject.Parse(LoginStatus);
                access_token = json["access_token"].Value<string>();
                mas_user_id = json["mas_user_id"].Value<string>();
                
                string MacString = mas_user_id + mobiles + content + sign + serial + access_token;
                string mac = GetMd5Hash(MacString);
                 T_StengStatus = Http_SendSms(Url, SentType, mas_user_id, mobiles, content, sign, serial, mac, ContentType);
               
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);

            }
            return T_StengStatus;
        }

       


        //登录验证
        public static string PostHttp_Login(string url,string LoginType, string Ecname, string Username, string Password, string contentType)
        {
            url += LoginType;
            string responseContent = "准备登录";
            string body = "ec_name=" + Ecname + "&user_name=" + Username + "&user_passwd=" + Password;

            responseContent = HttpPost(url, body, contentType);
            return responseContent;
        }

        //短信发送
        private static string Http_SendSms(string url,string senttype, string mas_user_id, string mobiles, string content, string sign, string seria, string mac,string contentType)
        {
            url += senttype;
            string body = "mas_user_id=" + mas_user_id + "&mobiles=" + mobiles + "&content=" + content + "&sign=" + sign + "&serial=" + seria + "&mac=" + mac;
           // Console.WriteLine("【连接】:" + url + body);
            string SentStatus = "准备发送";

            SentStatus = HttpPost(url, body, contentType);
            //SentStatus = WebPost(url, body, contentType);
            return SentStatus;
        }

        private static string Http_SendTSms(string url, string senttype, string mas_user_id,string tempId,string param ,string mobiles, string content, string sign, string seria, string mac, string contentType)
        {
            url += senttype;
            string body = "mas_user_id=" + mas_user_id + "&template_id=" + tempId+ "&params=" + param +"&mobiles=" + mobiles + "&content=" + content + "&sign=" + sign + "&serial=" + seria + "&mac=" + mac;
            //Console.WriteLine("【连接】:" + url + body);
            string SentStatus = "准备发送";

            SentStatus = HttpPost(url, body, contentType);
            //SentStatus = WebPost(url, body, contentType);
            return SentStatus;
        }


        //MD5计算mac
        public static string GetMd5Hash(String input)
        {
            if (input == null)
            {
                return null;
            }

            MD5 md5Hash = MD5.Create();

            // 将输入字符串转换为字节数组并计算哈希数据  
            byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));

            // 创建一个 Stringbuilder 来收集字节并创建字符串  
            StringBuilder sBuilder = new StringBuilder();

            // 循环遍历哈希数据的每一个字节并格式化为十六进制字符串  
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("X2"));
            }

            // 返回十六进制字符串  
            return sBuilder.ToString();
        }

        //HttpPost方法
        //body是要传递的参数,格式"roleId=1&uid=2"
        //post的cotentType填写:
        //"application/x-www-form-urlencoded"
        //soap填写:"text/xml; charset=utf-8"
        //http登录post
        private static string HttpPost(string Url,string Body,string ContentType)
        {
            string ResponseContent = "";
            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(Url);

            httpWebRequest.ContentType = "application/x-www-form-urlencoded"; ;
            httpWebRequest.Method = "POST";
            httpWebRequest.Timeout = 20000; //setInstanceFollowRedirects
            httpWebRequest.MediaType = "json";

            byte[] btBodys = Encoding.UTF8.GetBytes(Body);
            httpWebRequest.ContentLength = btBodys.Length;
            httpWebRequest.GetRequestStream().Write(btBodys, 0, btBodys.Length);

            try
            {


                HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream());
                ResponseContent = streamReader.ReadToEnd();

                httpWebResponse.Close();
                streamReader.Close();
                httpWebRequest.Abort();
                httpWebResponse.Close();
            }
            catch (Exception ex)
            {
                return  ex.Message;
            }
            return ResponseContent;
        }

        //HttpPost方法
        //body是要传递的参数,格式"roleId=1&uid=2"
        //post的cotentType填写:
        //"application/x-www-form-urlencoded"
        //soap填写:"text/xml; charset=utf-8"
        //http登录post
        private static string WebPost(string Url, string Body, string ContentType)
        {
            string ResponseContent = "";
            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(Url);

            httpWebRequest.ContentType = ContentType;
            httpWebRequest.Method = "POST";
            httpWebRequest.Timeout = 20000;

            byte[] btBodys = Encoding.UTF8.GetBytes(Body);
            httpWebRequest.ContentLength = btBodys.Length;
            httpWebRequest.GetRequestStream().Write(btBodys, 0, btBodys.Length);


            StreamReader stream = null;
            try
            {
                using (WebResponse response = httpWebRequest.GetResponse())
                {
                    stream = new StreamReader(response.GetResponseStream());
                }
            }
            catch (WebException e)
            {
                stream = new StreamReader(e.Response.GetResponseStream());
            }
            
            ResponseContent = stream.ReadToEnd();
            return ResponseContent;
        }
    }
}

我发送短信,调用方法
public void(){
Sendsms_Http_Form_Load("15111111111","第一");
Sendsms_Http_Form_Load("150123456789","第二条");
}
连续发送多次后出错,显示超时。
现在怎样写个方法让他这两条之间延迟发送。

aCHENa的主页 aCHENa | 初学一级 | 园豆:5
提问于:2016-08-05 11:09
< >
分享
最佳答案
0

可以写个JS方法执行完才能再次点击按钮就好了

JS代码如下(obj传按钮objMessage传提示)

 function Btn_Disable(obj,objMessage) 

var varReturn=false; 
 if(objMessage != null) 

 if(confirm(objMessage)) 

varReturn = true; 
obj.disabled = true; 
__doPostBack(obj.id); 
 } 
else 

varReturn=false; 

}
 else 

varReturn = true; 
obj.disabled = true; 
__doPostBack(obj.id); 

 return varReturn; 

收获园豆:20
小w威 | 初学一级 |园豆:37 | 2016-08-05 18:14
其他回答(4)
0

使用多线线程发送

收获园豆:20
CodeHsu | 园豆:5668 (大侠五级) | 2016-08-05 11:12

不会用多线程

支持(0) 反对(0) aCHENa | 园豆:5 (初学一级) | 2016-08-05 11:15

@aCHENa: 这个可以自行搜索相关资料

支持(0) 反对(0) CodeHsu | 园豆:5668 (大侠五级) | 2016-08-05 11:30
0

第一条发送了么?

收获园豆:30
顾晓北 | 园豆:10898 (专家六级) | 2016-08-05 11:13

有时第一条发送后会出错

支持(0) 反对(0) aCHENa | 园豆:5 (初学一级) | 2016-08-05 11:16

@aCHENa: 显示超时是短信提供服务的问题?

支持(0) 反对(0) 顾晓北 | 园豆:10898 (专家六级) | 2016-08-05 11:21

@顾晓北: 是的。移动平台有限速设置,超过限制就出错

支持(0) 反对(0) aCHENa | 园豆:5 (初学一级) | 2016-08-05 11:34

@aCHENa: 既然这样,那你就只有自己限制了,如果你是测试,你可以线程sleep一下。

支持(0) 反对(0) 顾晓北 | 园豆:10898 (专家六级) | 2016-08-05 11:36
0

写一个发送类封装一下发送方法,每次发送判断下与上次发送的时间间隔,如果不足你设置的最小时间间隔,sleep一下。

收获园豆:30
ArthurLi | 园豆:686 (小虾三级) | 2016-08-05 11:54
0

线程比较好处理,不会有卡顿的现象.

SOSOS's BLog | 园豆:125 (初学一级) | 2016-08-08 14:20
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册