现在做一个短信发送平台的接口,总之是接手别人的活,什么东西都不太清楚,所有的事情还得向远程合作的合作伙伴问。现在的问题是,在向远程数据库中插入数据的时候,表里面的内容是乱码的,但是我不知道如何在我现有的代码上去转码,而且也在网上搜了一些资料但似乎 都和我的问题不太一样。我还是先贴我的代码吧。using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ImApiDotNet;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Net;
using System.Text;
using System.IO;
using MySql.Data.MySqlClient;
using Chance.Common.DataModel;
namespace Chance.Common
{
public class LIinterface
{
//Quser_CompanyModel companyModel = new Quser_CompanyModel();
//Quser_PersonModel personModel = new Quser_PersonModel();
private string txtIp; public string TxtIp
{
get { return txtIp; }
set { txtIp = value; }
}
private string txtCode; public string TxtCode
{
get { return txtCode; }
set { txtCode = value; }
}
private string txtUserName; public string TxtUserName
{
get { return txtUserName; }
set { txtUserName = value; }
}
private string txtPassword; public string TxtPassword
{
get { return txtPassword; }
set { txtPassword = value; }
} private string txtDB; public string TxtDB
{
get { return txtDB; }
set { txtDB = value; }
} private string sismsid; public string Sismsid
{
get { return sismsid; }
set { sismsid = value; }
}
private string extcode; public string Extcode
{
get { return extcode; }
set { extcode = value; }
}
private string destaddr; public string Destaddr
{
get { return destaddr; }
set { destaddr = value; }
}
private string messagecontent; public string Messagecontent
{
get { return messagecontent; }
set { messagecontent = value; }
}
private int reqdeliveryreport; public int Reqdeliveryreport
{
get { return reqdeliveryreport; }
set { reqdeliveryreport = value; }
}
private int msgfmt; public int Msgfmt
{
get { return msgfmt; }
set { msgfmt = value; }
}
private int sendmethod; public int Sendmethod
{
get { return sendmethod; }
set { sendmethod = value; }
}
private DateTime requesttime; public DateTime Requesttime
{
get { return requesttime; }
set { requesttime = value; }
}
private string applicationid; public string Applicationid
{
get { return applicationid; }
set { applicationid = value; }
}
public LIinterface(string EXTCODE, string DESTADDR, string MESSAGECONTENT, DateTime REQUESTTIME, string APPLICATIONID)
{
this.sismsid = DateTime.Now.ToString("yyyyMMddHHmmssfff");
this.extcode = EXTCODE;
this.destaddr = DESTADDR;
this.messagecontent = MESSAGECONTENT;
this.reqdeliveryreport = 0;
this.msgfmt = 0;
this.sendmethod = 0;
this.requesttime = DateTime.Now;
this.applicationid = APPLICATIONID;
}
/// <summary>
/// 连接数据库字符串
/// </summary>
private static string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
public int ExecuteNonQueryer(string sql, params MySqlParameter[] para)
{
using (MySqlConnection conn = new MySqlConnection(connStr))
{
using (MySqlCommand cmd = new MySqlCommand(sql,conn))
{
if (para != null && para.Length != 0)
{
cmd.Parameters.AddRange(para);
}
if (conn.State == System.Data.ConnectionState.Closed)
{
conn.Open(); }
return cmd.ExecuteNonQuery();
}
} }
/// <summary>
/// 向数据库中插入要发送的相关记录
/// </summary>
/// <param name="SISMSID">UUID/GUID</param>
/// <param name="EXTCODE">扩展码</param>
/// <param name="DESTADDR">接收手机号码</param>
/// <param name="MESSAGECONTENT">发送内容</param>
/// <param name="REQDELIVERYREPORT">是否需要相关报告</param>
/// <param name="MSGFMT">消息类型</param>
/// <param name="SENDMETHOD">发送方式</param>
/// <param name="REQUESTTIME">短信发送时间</param>
/// <param name="APPLICATIONID">应用ID/插件ID</param>
/// <returns></returns>
public int InsertTo()
{
string sql = "Insert into sms_outbox (sismsid, extcode, destaddr, messagecontent, reqdeliveryreport,msgfmt,sendmethod,requesttime,applicationid) VALUES(@sismsid, '168', @destaddr, @messagecontent, 0,0,0,@requesttime,'168')";
MySqlParameter[] param ={
new MySqlParameter("@sismsid",this.Sismsid),
new MySqlParameter("@extcode",this.Extcode),
new MySqlParameter("@destaddr",this.destaddr),
new MySqlParameter("@messagecontent",this.Messagecontent),
new MySqlParameter("@reqdeliveryreport",this.reqdeliveryreport),
new MySqlParameter("@msgfmt",this.msgfmt),
new MySqlParameter("@sendmethod",this.sendmethod),
new MySqlParameter("@requesttime",this.Requesttime),
new MySqlParameter("@applicationid",this.applicationid),
}; return ExecuteNonQueryer(sql, param); }
}
}
这段代码就是向远程数据库的表中插入对方的手机号以及要发送的短信内容。
protected void btnRegUser_Click(object sender, EventArgs e)
{
if (Session["VNum"] !=null && Session["VNum"].ToString() == this.txtChkCode.Text.Trim())
{
if (!Chance.Business.Company.CompanyBll.UserNameIsExist(this.txtUserName.Text.Trim()))
{
Chance.Common.DataModel.Quser_CompanyModel model = new Chance.Common.DataModel.Quser_CompanyModel();
model.Qarea = Convert.ToInt32(this.ddlArea.SelectedValue.Trim());
model.QUserName = this.txtUserName.Text.Trim();
model.QUserPass = Chance.SysCode.Md5(this.txtPass1.Text.Trim());
model.QUserQuestion = this.txtQuestion.Text.Trim().Replace("'", "\"");
model.QUserAnswer = this.txtAnswer.Text.Trim().Replace("'", "\"");
model.QCompany = this.txtCompanyName.Text.Trim().Replace("'", "\"");
model.QAddress = this.txtAddress.Text.Trim().Replace("'", "\"");
model.Qhy = Convert.ToInt32(this.ddlHY.SelectedValue.Trim());
model.Qceo = this.txtCEO.Text.Trim().Replace("'", "\"");
model.Qtel = this.txtTel.Text.Trim().Replace("'", "\"");
model.Qfax = this.txtFax.Text.Trim().Replace("'", "\"");
model.Qlxr = this.txtLxr.Text.Trim().Replace("'", "\"");
model.Qmobile = this.txtMobile.Text.Trim().Replace("'", "\"");
model.Qemail = this.txtEmail.Text.Trim().Replace("'", "\"");
model.Qhomepage = this.txtHomePage.Text.Trim().Replace("'", "\"");
model.Qcontent = Chance.Common.StringUtil.ConvertToHtmlString(this.txtContent.Text);
model.QRegDate = DateTime.Now;
model.QloginCount = 0;
model.Qyzm = Chance.SysCode.GetRoudNum(6);
model.Qtj = "0";
model.Qtj_num = 0;
model.Qcheck = 1;
if (this.companyBll.InsertUserCompany(model))
this.Page.Response.Redirect("UserRegPost.aspx?type=company&Xid=" + model.QUserName, true);
else
{
this.ShowAlertScript("注册失败,请重新注册!");
}
}
else
{
this.ShowAlertScript("Sorry,您填写的会员账号已经被注册,请重新填写。");
}
}
else
this.ShowAlertScript("验证码填写不正确!");
}
.net
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ImApiDotNet;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Net;
using System.Text;
using System.IO;
using MySql.Data.MySqlClient;
using Chance.Common.DataModel;
namespace Chance.Common
{
public class LIinterface
{
//Quser_CompanyModel companyModel = new Quser_CompanyModel();
//Quser_PersonModel personModel = new Quser_PersonModel();
private string txtIp; public string TxtIp
{
get { return txtIp; }
set { txtIp = value; }
}
private string txtCode; public string TxtCode
{
get { return txtCode; }
set { txtCode = value; }
}
private string txtUserName; public string TxtUserName
{
get { return txtUserName; }
set { txtUserName = value; }
}
private string txtPassword; public string TxtPassword
{
get { return txtPassword; }
set { txtPassword = value; }
} private string txtDB; public string TxtDB
{
get { return txtDB; }
set { txtDB = value; }
} private string sismsid; public string Sismsid
{
get { return sismsid; }
set { sismsid = value; }
}
private string extcode; public string Extcode
{
get { return extcode; }
set { extcode = value; }
}
private string destaddr; public string Destaddr
{
get { return destaddr; }
set { destaddr = value; }
}
private string messagecontent; public string Messagecontent
{
get { return messagecontent; }
set { messagecontent = value; }
}
private int reqdeliveryreport; public int Reqdeliveryreport
{
get { return reqdeliveryreport; }
set { reqdeliveryreport = value; }
}
private int msgfmt; public int Msgfmt
{
get { return msgfmt; }
set { msgfmt = value; }
}
private int sendmethod; public int Sendmethod
{
get { return sendmethod; }
set { sendmethod = value; }
}
private DateTime requesttime; public DateTime Requesttime
{
get { return requesttime; }
set { requesttime = value; }
}
private string applicationid; public string Applicationid
{
get { return applicationid; }
set { applicationid = value; }
}
public LIinterface(string EXTCODE, string DESTADDR, string MESSAGECONTENT, DateTime REQUESTTIME, string APPLICATIONID)
{
this.sismsid = DateTime.Now.ToString("yyyyMMddHHmmssfff");
this.extcode = EXTCODE;
this.destaddr = DESTADDR;
this.messagecontent = MESSAGECONTENT;
this.reqdeliveryreport = 0;
this.msgfmt = 0;
this.sendmethod = 0;
this.requesttime = DateTime.Now;
this.applicationid = APPLICATIONID;
}
/// <summary>
/// 连接数据库字符串
/// </summary>
private static string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
public int ExecuteNonQueryer(string sql, params MySqlParameter[] para)
{
using (MySqlConnection conn = new MySqlConnection(connStr))
{
using (MySqlCommand cmd = new MySqlCommand(sql,conn))
{
if (para != null && para.Length != 0)
{
cmd.Parameters.AddRange(para);
}
if (conn.State == System.Data.ConnectionState.Closed)
{
conn.Open(); }
return cmd.ExecuteNonQuery();
}
} }
/// <summary>
/// 向数据库中插入要发送的相关记录
/// </summary>
/// <param name="SISMSID">UUID/GUID</param>
/// <param name="EXTCODE">扩展码</param>
/// <param name="DESTADDR">接收手机号码</param>
/// <param name="MESSAGECONTENT">发送内容</param>
/// <param name="REQDELIVERYREPORT">是否需要相关报告</param>
/// <param name="MSGFMT">消息类型</param>
/// <param name="SENDMETHOD">发送方式</param>
/// <param name="REQUESTTIME">短信发送时间</param>
/// <param name="APPLICATIONID">应用ID/插件ID</param>
/// <returns></returns>
public int InsertTo()
{
string sql = "Insert into sms_outbox (sismsid, extcode, destaddr, messagecontent, reqdeliveryreport,msgfmt,sendmethod,requesttime,applicationid) VALUES(@sismsid, '168', @destaddr, @messagecontent, 0,0,0,@requesttime,'168')";
MySqlParameter[] param ={
new MySqlParameter("@sismsid",this.Sismsid),
new MySqlParameter("@extcode",this.Extcode),
new MySqlParameter("@destaddr",this.destaddr),
new MySqlParameter("@messagecontent",this.Messagecontent),
new MySqlParameter("@reqdeliveryreport",this.reqdeliveryreport),
new MySqlParameter("@msgfmt",this.msgfmt),
new MySqlParameter("@sendmethod",this.sendmethod),
new MySqlParameter("@requesttime",this.Requesttime),
new MySqlParameter("@applicationid",this.applicationid),
}; return ExecuteNonQueryer(sql, param); }
}
}
这段代码就是向远程数据库的表中插入对方的手机号以及要发送的短信内容。
protected void btnRegUser_Click(object sender, EventArgs e)
{
if (Session["VNum"] !=null && Session["VNum"].ToString() == this.txtChkCode.Text.Trim())
{
if (!Chance.Business.Company.CompanyBll.UserNameIsExist(this.txtUserName.Text.Trim()))
{
Chance.Common.DataModel.Quser_CompanyModel model = new Chance.Common.DataModel.Quser_CompanyModel();
model.Qarea = Convert.ToInt32(this.ddlArea.SelectedValue.Trim());
model.QUserName = this.txtUserName.Text.Trim();
model.QUserPass = Chance.SysCode.Md5(this.txtPass1.Text.Trim());
model.QUserQuestion = this.txtQuestion.Text.Trim().Replace("'", "\"");
model.QUserAnswer = this.txtAnswer.Text.Trim().Replace("'", "\"");
model.QCompany = this.txtCompanyName.Text.Trim().Replace("'", "\"");
model.QAddress = this.txtAddress.Text.Trim().Replace("'", "\"");
model.Qhy = Convert.ToInt32(this.ddlHY.SelectedValue.Trim());
model.Qceo = this.txtCEO.Text.Trim().Replace("'", "\"");
model.Qtel = this.txtTel.Text.Trim().Replace("'", "\"");
model.Qfax = this.txtFax.Text.Trim().Replace("'", "\"");
model.Qlxr = this.txtLxr.Text.Trim().Replace("'", "\"");
model.Qmobile = this.txtMobile.Text.Trim().Replace("'", "\"");
model.Qemail = this.txtEmail.Text.Trim().Replace("'", "\"");
model.Qhomepage = this.txtHomePage.Text.Trim().Replace("'", "\"");
model.Qcontent = Chance.Common.StringUtil.ConvertToHtmlString(this.txtContent.Text);
model.QRegDate = DateTime.Now;
model.QloginCount = 0;
model.Qyzm = Chance.SysCode.GetRoudNum(6);
model.Qtj = "0";
model.Qtj_num = 0;
model.Qcheck = 1;
if (this.companyBll.InsertUserCompany(model))
this.Page.Response.Redirect("UserRegPost.aspx?type=company&Xid=" + model.QUserName, true);
else
{
this.ShowAlertScript("注册失败,请重新注册!");
}
}
else
{
this.ShowAlertScript("Sorry,您填写的会员账号已经被注册,请重新填写。");
}
}
else
this.ShowAlertScript("验证码填写不正确!");
}
.net
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//注册时短信发送 if (this.Xid != string.Empty && this.Type != string.Empty)
{
this.tbSendCode.Visible = true;
this.tbValidateCode.Visible = false;
switch (this.Type)
{
case "company":
Quser_CompanyModel model = (Quser_CompanyModel)companyBll.GetObjectByUserName(this.Xid, typeof(Quser_CompanyModel));
if (model != null && model.Qmobile.Trim() != string.Empty)
{
if (Chance.SysCode.QSMS > 0)
{
string SMSContext = System.Web.HttpUtility.UrlEncode("感谢您的注册,您在荆门金融网首次登陆验证码为:" + model.Qyzm + " ", System.Text.Encoding.GetEncoding("utf-8")); //this.ltSMS.Text = "<iframe name='sms' width='0' height='0' frameborder='0' scorlling='no' marginheight='0' marginwidth='0' src='" + FinanceWeb.BaseModule.UrlBase + "sms.aspx?txtTel=" + model.Qmobile + "&sContext=" + SMSContext + "'></iframe>"; LIinterface smsInfo = new LIinterface("168", model.Qmobile, SMSContext, DateTime.Now, "168");
smsInfo.InsertTo();
this.ltRegInfo.Text = "您会在10分钟内收到系统发送的<span class='td_red'>验证码短信</span>,请使用此验证码进行<span class='td_red'>首次</span>登陆!<br>如果您未收到验证码短信,请使用注册的用户名及密码登陆<span class='td_red'>再次获取验证码</span>!";
}
}
break;
case "personal":
Quser_PersonModel m_person = (Quser_PersonModel)b_person.GetObjectByUserName(this.Xid, typeof(Quser_PersonModel));
if (m_person != null && m_person.Qmobile.Trim() != String.Empty)
{
if (Chance.SysCode.QSMS > 0)
{
string SendContext = System.Web.HttpUtility.UrlEncode("感谢您的注册,您在荆门网首次登陆验证码为:" + m_person.Qyzm + " ", System.Text.Encoding.GetEncoding("gb2312"));
this.ltSMS.Text = "<iframe name='sms' width='0' height='0' frameborder='0' scorlling='no' marginheight='0' marginwidth='0' src='http://www.xfjr.gov.cn/sms.asp?fMobile=" + m_person.Qmobile + "&sContext=" + SendContext + "'></iframe>";
this.ltRegInfo.Text = "您在10分钟内收到系统发送的<span class='td_red'>验证码短信</span>,请使用此验证码进行<span class='td_red'>首次</span>登陆!<br>如果您未收到验证码短信,请使用注册的用户名记密码登陆<span class='td_red'>再次获取验证码</span>!";
}
}
break;
}
}
//登陆时验证码检验
else if (this.Act != string.Empty && this.Type != string.Empty)
{
this.tbSendCode.Visible = false;
this.tbValidateCode.Visible = true;
if (this.Type == "company")
hplkUrl.NavigateUrl = "UserRegPost.aspx?type=company&Xid=" + this.CurrentUser.LoginName;
else
hplkUrl.NavigateUrl = "UserRegPost.aspx?type=personal&Xid=" + this.CurrentUser.LoginName;
} else
{
this.tbSendCode.Visible = false;
this.tbValidateCode.Visible = false;
}
}
}
这段代码就是在用户注册成功后把相应的短信内容发送到用户手机上去的。
现是现在始终收不到短信,而且在远程的MySQL数据库里看到的短信内容是乱码的,问远程那边的人,他们说是因为我没有转码,需要转码成UTF-8的才行,但我的代码中不是已经转成UTF-8的了吗?为什么还会出现 这种乱码?
不过是在 winform 中的
web 中不确定是不是这样
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 这是加在前台页面的