wzp
2021-07-30 afda40ef498cca59e58b2b6869deae90f3c13de5
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;
using System.Xml;
 
namespace ThreadTestSMGW
{
    public class SMSHandler
    {
        public static string SITEURL = System.Configuration.ConfigurationManager.AppSettings["URL"];
        public string errmsg = "";
 
        /// <summary>
        /// 发送短信
        /// </summary>
        /// <param name="account"></param>
        /// <param name="password"></param>
        /// <param name="extendNo"></param>
        /// <param name="mobileNo"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public string SendSMS(string account, string password, string extendNo, string mobileNo, string content)
        {
            
            if ( string.IsNullOrEmpty(account) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(mobileNo) || string.IsNullOrEmpty(content))
            {
                //log4netService.Info(userid+account+password+mobileNo+content);
                errmsg = "参数不全";
                return errmsg;
            }
 
            /*
            string postData = string.Format("action=send&account=账号&password=密码&mobile=15023239810,13527576163&content=内容&extno=1069012345");  
            */
            //string requestUrl = string.Format("http://8.129.227.30:7862/sms");
            string requestUrl = string.Format(SITEURL+"/sms");
            StreamReader reader;
            Uri requestUri = new Uri(requestUrl);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.KeepAlive = true;
            request.Timeout = 1000000;
            StringBuilder data = new StringBuilder();
            data.Append("action=send");
            data.Append(string.Format("&account={0}", account));
            data.Append(string.Format("&password={0}", password));
            data.Append(string.Format("&extno={0}", extendNo));
            data.Append(string.Format("&mobile={0}", mobileNo));
            data.Append(string.Format("&content={0}", content+DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")));
 
            //byte[] byteData = UTF8Encoding.GetEncoding("GB212").GetBytes(data.ToString());
            byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString());//utf编码格式显性转换
            request.ContentLength = byteData.Length;
            // 开始请求
            using (Stream postStream = request.GetRequestStream())
            {
                postStream.Write(byteData, 0, byteData.Length);
            }
 
 
            WebResponse response = (HttpWebResponse)request.GetResponse();
            //reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("GB2312"));
            reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
 
            StringBuilder builder = new StringBuilder();
            char[] buffer = new char[0x400];
            int charCount = 0;
            while ((charCount = reader.Read(buffer, 0, buffer.Length)) > 0)
            {
                builder.Append(buffer, 0, charCount);
            }
            reader.Close();
            string result = builder.ToString();
            if (string.IsNullOrEmpty(result))
            {
                errmsg = "未知异常";
                return errmsg;
            }
            XmlDocument docxml = new XmlDocument();
            docxml.LoadXml(result);
 
            //DataSql ds = new DataSql();
 
            string r = docxml.SelectSingleNode("returnsms/returnstatus").InnerText;
            if (r == "Success")
            {
                errmsg = docxml.SelectSingleNode("returnsms/taskID").InnerText;
                //ds.UpdateSql(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"),id);
                return errmsg;// +","+DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
 
            }
            else
            {
                errmsg = docxml.SelectSingleNode("returnsms/message").InnerText;
                //ds.UpdateSql(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), id);
                return errmsg;// +"," + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
            }
        }
 
 
 
        /// <summary>
        /// 点对点短信发送
        /// </summary>
        /// <param name="account"></param>
        /// <param name="password"></param>
        /// <param name="extendNo"></param>
        /// <param name="mobileConentList"></param>
        /// <returns></returns>
        public string SendSMS(string account, string password, string extendNo, string mobileConentList)
        {
 
            if (string.IsNullOrEmpty(account) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(mobileConentList))
            {
                //log4netService.Info(userid+account+password+mobileNo+content);
                errmsg = "参数不全";
                return errmsg;
            }
            string requestUrl = string.Format(SITEURL + "/sms");
            StreamReader reader;
            Uri requestUri = new Uri(requestUrl);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.KeepAlive = true;
            request.Timeout = 1000000;
            StringBuilder data = new StringBuilder();
            data.Append("action=p2p");
            data.Append(string.Format("&account={0}", account));
            data.Append(string.Format("&password={0}", password));
            data.Append(string.Format("&extno={0}", extendNo));
            data.Append(string.Format("&mobileContentList={0}", mobileConentList));
            data.Append(string.Format("&rt=json"));
 
            //byte[] byteData = UTF8Encoding.GetEncoding("GB212").GetBytes(data.ToString());
            byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString());//utf编码格式显性转换
            request.ContentLength = byteData.Length;
            // 开始请求
            using (Stream postStream = request.GetRequestStream())
            {
                postStream.Write(byteData, 0, byteData.Length);
            }
 
 
            WebResponse response = (HttpWebResponse)request.GetResponse();
            //reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("GB2312"));
            reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
 
            StringBuilder builder = new StringBuilder();
            char[] buffer = new char[0x400];
            int charCount = 0;
            while ((charCount = reader.Read(buffer, 0, buffer.Length)) > 0)
            {
                builder.Append(buffer, 0, charCount);
            }
            reader.Close();
            string result = builder.ToString();
            if (string.IsNullOrEmpty(result))
            {
                errmsg = "未知异常";
                return errmsg;
            }
            return result;
 
 
            //XmlDocument docxml = new XmlDocument();
            //docxml.LoadXml(result);
 
 
 
            //string r = docxml.SelectSingleNode("returnsms/returnstatus").InnerText;
            //if (r == "Success")
            //{
            //    errmsg = docxml.SelectSingleNode("returnsms/taskID").InnerText;
                
            //    return errmsg;
 
            //}
            //else
            //{
            //    errmsg = docxml.SelectSingleNode("returnsms/message").InnerText;
                
            //    return errmsg;
            //}
        }
 
 
        public string Report(string account, string password)
        {
 
            if (string.IsNullOrEmpty(account) || string.IsNullOrEmpty(password))
            {
                //log4netService.Info(userid+account+password+mobileNo+content);
                errmsg = "参数不全";
                return errmsg;
            }
 
            /*
            string postData = string.Format("action=send&account=账号&password=密码&mobile=15023239810,13527576163&content=内容&extno=1069012345");  
            */
            string requestUrl = string.Format(SITEURL);
            StreamReader reader;
            Uri requestUri = new Uri(requestUrl);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.KeepAlive = true;
            request.Timeout = 1000000;
            StringBuilder data = new StringBuilder();
            data.Append("action=report");
            data.Append(string.Format("&account={0}", account));
            data.Append(string.Format("&password={0}", password));
            
            //byte[] byteData = UTF8Encoding.GetEncoding("GB212").GetBytes(data.ToString());
            byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString());//utf编码格式显性转换
            request.ContentLength = byteData.Length;
            // 开始请求
            using (Stream postStream = request.GetRequestStream())
            {
                postStream.Write(byteData, 0, byteData.Length);
            }
 
 
            WebResponse response = (HttpWebResponse)request.GetResponse();
            //reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("GB2312"));
            reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
 
            StringBuilder builder = new StringBuilder();
            char[] buffer = new char[0x400];
            int charCount = 0;
            while ((charCount = reader.Read(buffer, 0, buffer.Length)) > 0)
            {
                builder.Append(buffer, 0, charCount);
            }
            reader.Close();
            string result = builder.ToString();
            if (string.IsNullOrEmpty(result))
            {
                errmsg = "未知异常";
                return errmsg;
            }
            return "状态报告" + "," + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
        }
    }
}