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); } } /// /// Post请求 /// /// 日志名 /// 手机号码 /// url /// 参数 /// 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(); } } } } }