短信发送
时间:2011-04-20 来源:陌香
中国短信网的发送接口:
/// <summary>
/// 发送短信并返回结果
/// </summary>
/// <param name="hm">号码</param>
/// <param name="nr">内容</param>
/// <returns></returns>
public static String fs(string hm, string nr)
{
StringBuilder sbTemp = new StringBuilder();
sbTemp.Append("uid=*****&pwd=*****&mobile=" + hm + "&content=" + nr);
byte[] bTemp = System.Text.Encoding.GetEncoding("GBK").GetBytes(sbTemp.ToString());
String postReturn = Define.doPostRequest("http://http.c123.com/tx/", bTemp);
return postReturn;
}
//POST方式发送得结果
public static String doPostRequest(string url, byte[] bData)
{
System.Net.HttpWebRequest hwRequest;
System.Net.HttpWebResponse hwResponse;
string strResult = string.Empty;
try
{
hwRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
hwRequest.Timeout = 5000;
hwRequest.Method = "POST";
hwRequest.ContentType = "application/x-www-form-urlencoded";
hwRequest.ContentLength = bData.Length;
System.IO.Stream smWrite = hwRequest.GetRequestStream();
smWrite.Write(bData, 0, bData.Length);
smWrite.Close();
}
catch (System.Exception err)
{
WriteErrLog(err.ToString());
return strResult;
}
//get response
try
{
hwResponse = (HttpWebResponse)hwRequest.GetResponse();
StreamReader srReader = new StreamReader(hwResponse.GetResponseStream(), Encoding.ASCII);
strResult = srReader.ReadToEnd();
srReader.Close();
hwResponse.Close();
}
catch (System.Exception err)
{
WriteErrLog(err.ToString());
}
return strResult;
}
public static void WriteErrLog(string strErr)
{
Console.WriteLine(strErr);
System.Diagnostics.Trace.WriteLine(strErr);
}
这个网站只能接收到别人回复的短信,不能设置接收的号码,跟我的需求不同,就没继续使用下去,过几天我用其他的接口实现了短信查询功能后,再贴出来…