protected void Page_Load(object sender, EventArgs e){
String userName = "注册用户名";
String key = "接口鉴权KEY";
String mobileNos = "13800000000";
String content = "验证码:8888";
string url = "https://api.smslong.cn/";
string post = string.Format("user={0}&key={1}&mobile={2}&content={3}", userName, key, mobileNos, content);
Response.Write(PostData(url, post));
}
public static string PostData(string posturl, string postData){
//ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
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
{
//ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
//ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
// 设置参数
//ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
request = WebRequest.Create(posturl) as HttpWebRequest;
CookieContainer cookieContainer = new CookieContainer();
request.CookieContainer = cookieContainer;
request.AllowAutoRedirect = true;
request.Method = "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)
{
return ex.Message;
}
}