#region 调用短信接口
public ActionResult Mobile(string Tel)//调用接口
{
Random rm = new Random();
int i;
string str = string.Empty;
for (int p = 0; p < 6; p++)
{
i = Convert.ToInt32(rm.NextDouble() * 10);
str += i;
}
System.Web.HttpContext.Current.Session["Code"] = str;
//string stringSendResult = SendMobileCode(Tel, str);//方法一
string stringSendResult2 = Send(Tel, "验证码是:" + str);
return Json(true, JsonRequestBehavior.AllowGet);//短信发送成功
}
#endregion
#region 方法一(可能有些问题,走不通的先试用方法二)
public string SendMobileCode(string Mobile, string number)
{
if (string.IsNullOrEmpty(Mobile)) return "发送失败";
var str = "您的验证码为:" + number + ",5分钟有效,请尽快认证";
str = HttpUtility.UrlEncode(str, Encoding.GetEncoding("gb2312"));
string Chuo = DateTime.Now.ToString("yyyyMMddHHmmss");//时间戳
string MD5Chuo = "wdyzmwdyzm@456321" + Chuo;//账号 + 密码 + 时间戳 的MD5
string sign = Encrypt(MD5Chuo);
var url = "http://这里是地址/接口方法?参数1=send&参数2=111&时间戳=" + chuo + "&签名=" + sign + "&手机号=" + tel + "&发送内容=" + number + "&参数3=&参数4=";
var rst = (HttpWebRequest)WebRequest.Create(url);
var rsp = (HttpWebResponse)rst.GetResponse();
var reader = new StreamReader(rsp.GetResponseStream(), Encoding.GetEncoding("gb2312"));
reader.ReadToEnd();
reader.Close();
return number;
}
#endregion
#region 方法二
#region
public string Send(string tel, string number)
{
string chuo = DateTime.Now.ToString("yyyyMMddHHmmss");//时间戳
string MD5s = "wdyzmwdyzm@456321" + chuo;//账号 + 密码 + 时间戳 的MD5
string sign = Encrypt(MD5s);
string posturl = "http://这里是地址/接口方法?参数1=send&参数2=111&时间戳=" + chuo + "&签名=" + sign + "&手机号=" + tel + "&发送内容=" + number + "&参数3=&参数4=";
return GetPage(posturl);//以post的形式发送出去
}
#endregion
#region
public string GetPage(string posturl)
{
Stream outstream = null;
Stream instream = null;
StreamReader sr = null;
HttpWebResponse response = null;
HttpWebRequest request = null;
Encoding encoding = Encoding.UTF8;
//byte[] data = encoding.GetBytes(postData);
// 准备请求...
try
{
// 设置参数
request = WebRequest.Create(posturl) as HttpWebRequest;
CookieContainer cookieContainer = new CookieContainer();
request.CookieContainer = cookieContainer;
request.AllowAutoRedirect = true;
request.Method = "POST";//post的形式
request.ContentType = "application/x-www-form-urlencoded";
//request.ContentLength = data.Length;
outstream = request.GetRequestStream();
//outstream.Write(data, 0, data.Length);
outstream.Close();
//发送请求并获取相应回应数据
response = request.GetResponse() as HttpWebResponse;
//直到request.GetResponse()程序才开始向目标网页发送Post请求
instream = response.GetResponseStream();
sr = new StreamReader(instream, encoding);
//返回结果网页(html)代码
string content = sr.ReadToEnd();
string err = string.Empty;
return content;
}
catch (Exception ex)
{
string err = ex.Message;
//Response.Write(err);
//return string.Empty;
return err;
}
}
#endregion
#endregion
ps:已简化