C#植入友商网在线服务并实现同步用户注册接口实现
C#植入友商网在线服务并实现同步用户注册接口实现
请先下载《友商网同步用户和单点登录接口v3.0说明书》,阅读后再理解下面的代码.
1.建立TUser类
/// <summary>
/// 友商网提供的xml文件建立User类
/// </summary>
public class TUser
{
private string _userAccount = "";
private string _Password = "";
private string _realName = "";
private string _gender = "";
private string _email = "";
private string _mobile = "";
private string _phone = "";
private string _address = "";
private string _bindTo = "";
private string _bindAsAdmin = "";
public string userAccount { get { return _userAccount; } set { _userAccount = value; } }
public string password { get { return _Password; } set { _Password = value; } }
public string realName { get { return _realName; } set { _realName = value; } }
public string gender { get { return _gender; } set { _gender = value; } }
public string email { get { return _email; } set { _email = value; } }
public string mobile { get { return _mobile; } set { _mobile = value; } }
public string phone { get { return _phone; } set { _phone = value; } }
public string address { get { return _address; } set { _address = value; } }
public string bindTo { get { return _bindTo; } set { _bindTo = value; } }
public string bindAsAdmin { get { return _bindAsAdmin; } set { _bindAsAdmin = value; } }
}
//如转载请注明出处www.csframework.com
}
2.注册用户的类(YoushangUserRegister)
/// <summary>
/// 调用友商网注册用户接口
/// </summary>
public class YoushangUserRegister
{
private string _partnerID;
private string _aesKey;
public YoushangUserRegister(string partnerID, string aesKey)
{
_partnerID = partnerID;
_aesKey = aesKey;
}
/// <summary>
/// 注册用户
/// </summary>
public bool Register(TUser user)
{
string url = "http://agent.youshang.com/federation/commonservice/servicePortal.do"; //友商网接口
string urlParam = "partnerId={0}&cipherCode={1}&iv={2}"; //接口的URL参数
string iv = AES_Crypto.GetIv(16); //生成iv代码
string xmlUser = this.getUserXML(user); //TUser对象转换为xml文件
//AES加密用户XML数据
string cipherCode = AES_Crypto.AESEncrypt(xmlUser, _aesKey, iv);
cipherCode = HttpUtility.UrlEncode(cipherCode);//HTML ENCODE
iv = HttpUtility.UrlEncode(iv);//HTML ENCODE
urlParam = string.Format(urlParam, _partnerID, cipherCode, iv);
return this.HttpPost(url, urlParam); //提交数据....
}
/// <summary>
/// 调用友商网提供的注册用户接口,发送XML数据及接收。
/// </summary>
/// <param name="url">接口URL地址</param>
/// <param name="param">URL参数,xml已加密</param>
/// <returns></returns>
private bool HttpPost(string url, string param)
{
WebRequest req = WebRequest.Create(url);
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(param);
req.ContentLength = bytes.Length; //发送的请求(Request)数据大小
System.IO.Stream os = req.GetRequestStream();
os.Write(bytes, 0, bytes.Length); //发送...
os.Close();
System.Net.WebResponse resp = req.GetResponse(); //等待友商网的服务器回应.
if (resp == null) return false;
System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
string retXML = sr.ReadToEnd().Trim();
return IsSuccess(retXML);
}
/// <summary>
/// 检查友商网返回的XML数据.
/// </summary>
/// <param name="retXML"></param>
/// <returns></returns>
private bool IsSuccess(string retXML)
{
XmlDocument xml = new XmlDocument();
xml.LoadXml(retXML);
XmlNode node = xml.SelectSingleNode("response/status");
//返回结果: "0":表示成功; "1":表示失败
return node.InnerText.Trim() == "0";
}
/// <summary>
/// 友商网注册用户的XML文件规范
/// </summary>
/// <param name="user">将user转换为xml文件</param>
/// <returns></returns>
private string getUserXML(TUser user)
{
string xml =
"<?xml version=\"1.0\" encoding = \"utf-8\"?>" +
"<request>" +
"<type>userSync</type>" +
"<data>" +
"<attr name=\"userAccount\">{0}</attr> " +
"<attr name=\"realName\">{1}</attr>" +
"<attr name=\"gender\">{2}</attr>" +
"<attr name=\"email\">{3}</attr>" +
"<attr name=\"mobile\">{4}</attr>" +
"<attr name=\"phone\">{5}</attr>" +
"<attr name=\"address\">{6}</attr>" +
"<attr name=\"bindTo\">{7}</attr>" +
"<attr name=\"bindAsAdmin\">{8}</attr>" +
"</data>" +
"</request>";
xml = string.Format(xml, user.userAccount, user.realName, user.gender,
user.email, user.mobile, user.phone, user.address,
user.bindTo, user.bindAsAdmin);
return xml;
//如转载请注明出处www.csframework.com
}
}
3. 测试方法
友商网提供测试平台接口:
http://agent.youshang.com/federation/commonservice/servicePortal.do?partnerId=value&cipherCode=value&iv=value
请先修改本地的DNS,改为:219.133.170.202,DNS自动转发到测试接口,否则调用实际环境测试会出错。建立一个WebSite工程,在default.aspx页面加一些输入框,如下图:
提交按钮代码:
protected void Button1_Click(object sender, EventArgs e)
{
TUser user = new TUser();
user.address = txtAddress.Text;
user.email = txtEmail.Text;
user.gender = txtGender.Text;
user.mobile = txtMobile.Text;
user.password = txtPassword.Text;
user.phone = txtPhone.Text;
user.realName = txtRealName.Text;
user.userAccount = txtUserAccount.Text;
//注册用户对象
YoushangUserRegister register = new YoushangUserRegister(
SecurityClass.Instance.GetPartnerID(), //友商网伙伴编号
SecurityClass.Instance.GetAESKey()); //AES加密密码,与友商网开发组约定AES密码
bool ret = register.Register(user);
if (ret)
{
this.Response.Write("插入友商后台数据库成功!");
}
else
{
this.Response.Write("插入友商后台数据失败!");
}
}
注意: 必须有友商网伙伴编号及约定AES密码.否则友商网后台无法解释加密的XML文件!!!
《友商网同步用户和单点登录接口v3.0说明书》下载