public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
string phone = context.Request.QueryString["phone"];
string Url = "http://restful.31huiyi.com/api/Infrastructures/SendRandomCode";
Random Rdm = new Random();
//随机数
int iRdm = Rdm.Next(100000, 999999);
//string postdate = "{EmailOrMobile='15221222951',code='" + iRdm + "',UserId=0, RandomCodeType=1, contentTemplate='您本次手机验证的验证码是{code} 【31会议】'}";
string postdate = JsonConvert.SerializeObject(new { EmailOrMobile = "" + phone + "", code = "" + iRdm + "", UserId = "0", RandomCodeType = "1", contentTemplate = "您参加的四季沐歌春节红包活动的短信验证码是{code}。(四季沐歌)——平台支持:【31会议】" });
JObject jo = JObject.Parse(GetHtml(Url,postdate));
context.Response.Write(jo["Body"]);
}
public static string GetHtml(string posturl, string postData)
{
string date = DateTime.Now + ": " + System.Guid.NewGuid().ToString();
string appauth = "{"APPID":40032,"httpMethod":"POST","ClientIP":"" + HttpContext.Current.Request.UserHostAddress + "","WebRequestGuid":"" + date + "","AppRequestGuid":null,"RequestURL":"" + posturl + "","WebRequestURL":null,"WebRequestUrlReferrer":null,"APPAuth":""}";
Stream outstream = null;
Stream instream = null;
StreamReader sr = null;
HttpWebResponse response = null;
HttpWebRequest request = null;
Encoding encoding = System.Text.Encoding.GetEncoding("UTF-8");
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";
request.ContentType = "application/json";
request.Headers.Add("X_31HuiYi_LoginCookie", "");
request.Headers.Add("X_31HuiYi_AppAuth", "{"APPID":40032,"httpMethod":"POST","ClientIP":"" + HttpContext.Current.Request.UserHostAddress + "","WebRequestGuid":"" + date + "","AppRequestGuid":null,"RequestURL":"" + posturl + "","WebRequestURL":null,"WebRequestUrlReferrer":null,"APPAuth":"" + System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(appauth + "a9p5_shzhidi_cuijason_33409_31huiyi_com", "MD5").ToLower() + ""}");
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;
return string.Empty;
}
}
获取:
$("#huoqu").click(function () {
$.ajax({
type: 'post',
url: 'yanzhenma.ashx?phone=' + $("#tel").val() + '',
success: function (msg) {
yanzhenma = msg;
$("#huoqu").val("已发送!");
$("#huoqu").attr({ "disabled": "disabled" });
}
})
})