现在部份的网站上都有发送短信验证码的功能,在电话号码表单内输入手机号码后,点击获取验证码即收到短信的验证码;当前的表单无刷新,基本都是以ajax的方式实现,通常ajax请求的也是一个URL
现在本人想省去用户输入手机号码,直接从手机端获取,并填充,模拟发出获取验证码的请求,让用户省去输入手机号码;使用的是httpwebrequest的方法,发出请求后,未能正确的返回状态,手机也未收到短信验证码。但在网页中点击获取验证码,则正常; 不知道是哪里出错了,下面是请求方法:
public static string RequestUrl(string url)
{
System.Net.ServicePointManager.DefaultConnectionLimit = 50;
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);
httpRequest.Accept = "text/plain, */*; q=0.01";
httpRequest.Timeout = 5000;
httpRequest.Method = "POST";
httpRequest.KeepAlive = false;
httpRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E)";
httpRequest.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
string result = string.Empty;
System.GC.Collect();
HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
try
{
using (StreamReader sr = new StreamReader(httpResponse.GetResponseStream(), System.Text.Encoding.GetEncoding("utf-8")))
{
result = sr.ReadToEnd();
result = result.Replace("\r", "").Replace("\n", "").Replace("\t", "");
int status = (int)httpResponse.StatusCode;
sr.Close();
}
httpResponse.Close();
}
catch (Exception)
{
result = "";
throw;
}
finally
{
httpResponse.Close();
}
return result;
}
}
网页上获取验证码的ajax方法
$.ajax({ url : url, data : "msisdn="+$("#msisdn").val(), type:"POST", success : function(rs) { var ms="重新获取短信验证码"; if("Y"!=rs){ if("200003"==rs.split(",")[1]){ ms="获取验证码过于频繁,请稍后再试!"; }else{ ms="获取验证码失败"; } } } });
你找到问题原因了吗?