[求助]怎么实现基于电信API的学生成绩短信发送平台
求高手帮忙修改,谢谢namespace WindowsFormsApplication4
{
class Class1
{
/// <summary>
/// 短信测试
/// </summary>
/// <returns></returns>
public string SmsTest()
{
//创建url连接对象
Uri servletURL = new Uri("http://125.64.11.43:8080/udt-see/StartServiceServlet");
//创建客户端请求对象
WebClient client = new WebClient();
//设置请求头部样式
client.Headers.Add("Content-type", "text/html; charset=utf-8");
//根据指定格式获得时间戳
string dt = DateTime.Now.ToString("yyyyMMddHHmmss");
//使用3des将apid和时间戳,密钥加密
string key = Class1.Encrypt3Des(dt + "用户apid", "密钥");
//使用json对象拼接参数(也可以自己根据json数据格式,自己使用字符串拼接)
JSON js = new JSON();
js.addItem("APID", "用户apid");
js.addItem("Key", key);
for (int x = 1; x <= 7; x++)
{
int sno = 0;
SqlConnection lo_conn = new SqlConnection("Server=服务器名字或IP;Database=数据库名字;uid=用户名;pwd=密码");
lo_conn.Open();
SqlCommand lo_cmd = new SqlCommand(); //创建命令对象
lo_ = "EXEC SQL SELECT mobile;INTO :smobile;FROM student;WHERE no=:sno;EXEC SQL SELECT course,score;INTO :scourse,:sscore;FROM sc;WHERE no=:sno"; //写SQL语句
lo_cmd.Connection = lo_conn; //指定连接对象,即上面创建的
SqlDataAdapter dbAdapter = new SqlDataAdapter(lo_cmd); //注意与上面的区分开
DataSet ds = new DataSet(); //创建数据集对象
dbAdapter.Fill(ds); //用返回的结果集填充数据集,这个数据集可以被能操作数据的控件DataBind
lo_conn.Close();
sno = sno++;
js.addItem("Num", "");
js.addItem("Msg", "test");
}
js.addItem("TimeStamp", dt);
js.addItem("IsReport", "1");
js.addItem("StatusReportUrl", "");
js.addItem("Parm1", "");
js.addItem("Parm2", "");
js.addItem("Parm3", "");
JSON json = new JSON();
json.addItem("serviceId", "2010051702");
json.addItems("params", js.ToString());
//发送客户端请求到服务器,并获得返回值
return client.UploadString(servletURL.ToString(), "POST", json.ToString());
}
/// <summary>
/// 3des加密算法
/// </summary>
/// <param name="strString"></param>
/// <param name="strKey"></param>
/// <returns></returns>
public static string Encrypt3Des(string strString, string strKey)
{
byte[] strbyte = Convert.FromBase64String(strKey);
strKey = Encoding.UTF8.GetString(strbyte);
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
des.Key = ASCIIEncoding.ASCII.GetBytes(strKey);
des.Mode = CipherMode.ECB;
ICryptoTransform desEncrypt = des.CreateEncryptor();
byte[] Buffer = ASCIIEncoding.ASCII.GetBytes(strString);
return Convert.ToBase64String(desEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
}
}
namespace WindowsFormsApplication4
{
class JSON
{
//是否成功
private bool success;
//错误提示信息
private string error;
//总记
private int totalCount;
//数据
private string singleInfo;
private ArrayList arrData;
#region 初始化JSON的所有对象
public JSON()
{
error = string.Empty;
singleInfo = string.Empty;
totalCount = 0;
success = false;
arrData = new ArrayList();
}
#endregion
#region 重置JSON的所有对象
public void ResetJSON()
{
error = string.Empty;
singleInfo = string.Empty;
totalCount = 0;
success = false;
arrData.Clear();
}
#endregion
#region 对象与对象之间分割符
public void addItemOk()
{
arrData.Add("<br>");
}
#endregion
#region 在数组里添加key,value
public void addItem(string name, string value)
{
arrData.Add(name +":" + "\"" + value + "\"");
}
#endregion
#region 在数组里添加key,value
public void addItems(string name, string value)
{
arrData.Add(name + ":" + value);
}
#endregion
#region 返回组装好的json字符串
public override string ToString()
{
StringBuilder sb = new StringBuilder();
int index = 0;
sb.Append("{");
if (arrData.Count <= 0)
{
sb.Append("}]");
}
else
{
foreach (string val in arrData)
{
index++;
if (val != "<br>")
{
sb.Append(val + ",");
}
else
{
sb = sb.Replace(",", "", sb.Length - 1, 1);
sb.Append("},");
if (index < arrData.Count)
{
sb.Append("{");
}
}
}
sb = sb.Replace(",", "", sb.Length - 1, 1);
}
sb.Append("}");
return sb.ToString();
}
#endregion
public string errorS
{
get
{
return this.error;
}
set
{
this.error = value;
}
}
public bool successS
{
get
{
return this.success;
}
set
{
this.success = value;
}
}
public int totalCountS
{
get
{
return this.totalCount;
}
set
{
this.totalCount = value;
}
}
}
}