zhaojs
2023-09-27 74098f1401afe40f961d1d167bb18dd0a71c4d59
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
using CommonUtil;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Web;
 
namespace SmsSendTool
{
   public class SmsSendApi
    {
        private string _strjson = string.Empty;
        private string _username = string.Empty;
        private string _pwd = string.Empty;
 
        public SmsSendApi(string strjson,string username,string pwd)
        {
            _strjson = strjson;
            _username = username;
            _pwd = pwd;
        }
 
        public void SendKTApi(ref string strResult)
        {
            SmsMsgEntity msg = new SmsMsgEntity(_strjson);
 
            string url = "http://114.215.196.145/variableSmsApi";//营销的
            string urlText = "username=" + _username + "&password=" + _pwd + "&mobile=" + msg.Mobile + "&content=" + HttpUtility.UrlEncode(msg.Content, Encoding.UTF8) + "&xh=999";
            strResult = PostAsync(url, urlText, msg.Mobile, "", "");
            LogUtil.Info(strResult + ":" + msg.Mobile, "客通" + _username);
            if (!strResult.Contains("1,"))
            {
                LogUtil.Error(msg.Mobile + ":" + strResult, "客通异常" + _username);
            }
        }
 
        /// <summary>
        /// Post请求
        /// </summary>
        /// <param name="logname">日志名</typeparam>
        /// <param name="sendphones">手机号码</param>
        /// <param name="url">url</param>
        /// <param name="data">参数</param>
        /// <returns></returns>
        private string PostAsync(string url, string data, string sendphones, string logname, string contenttype)
        {
 
            HttpWebResponse httpWebResponse = null;
            StreamReader sr = null;
            Stream newStream = null;
            try
            {
                HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
 
                httpWebRequest.Method = "POST";
 
                //   httpWebRequest.Timeout = 600000;
 
                httpWebRequest.ContentType = contenttype.Length == 0 ? "application/x-www-form-urlencoded" : contenttype;    //表头的格式必须要写,否则请求响应的页面得不到要传递的值
 
                byte[] SomeBytes = System.Text.Encoding.Default.GetBytes(data);//传递的值 
 
                httpWebRequest.ContentLength = SomeBytes.Length;
 
                newStream = httpWebRequest.GetRequestStream();//把传递的值写到流中   
 
                newStream.Write(SomeBytes, 0, SomeBytes.Length);
 
                newStream.Close();//必须要关闭 请求
 
                httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                sr = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.UTF8);
                string retValue = sr.ReadToEnd();
                return retValue;
            }
            catch (Exception ex)
            {
                LogUtil.Error(ex.ToString() + "===" + sendphones, logname);
                return "";
            }
            finally
            {
                if (sr != null)
                {
                    sr.Close();
                }
                if (httpWebResponse != null)
                {
                    httpWebResponse.Close();
                }
                if (newStream != null)
                {
                    newStream.Close();
                }
            }
        }
    }
}