开发日记:PDO统一接口开发规范1.0 事例
这个星期做了一个API接口整合的程序大家还是看程序把!
PDO统一接口开发规范1.0 加密方式
密码加密是MD5 32位加密
syskey 加密是32位安全码
功能描述:
1: 从A网站登录到B网站(A网站也同时登录)
2: 从B网站登录到A网站(B网站也同时登录)
注意:
1:俩个站点的syskey要一样。
2:俩个站点的用户名和密码要一样。
3:俩个站点是用XML传输的 编码是gb2312
使用说明:
1:在项目登录页中添加如下代码:
#region 动易,动网,Oblog 整合 编写人:JONE 编写时间:2011年1月13日 16:54:30
if (PDOAction.Instance().Enabled){
PDOAction.Instance().Login(Username.Text, hashPassword, 1);
}
#endregion
请求
格式:
POST HTTP/1.1 Content-Type: text/xml; charset=gb2312
<?xml version="1.0" encoding="gb2312"?>
<root>
<appid>程序标识</appid>
<action>操作类型</action>
<syskey>安全码</syskey>
<username>用户名</username>
<可选元素列表 />
</root>
提供 API 请求类 (PDOAction类)
/// <summary> /// 提供API请求的类 /// </summary> public static class Request { public static bool CheckEmail(string userName, string email) { ApiData data = new ApiData(); data.SpeItems[1, 1] = "checkemail"; data.SpeItems[5, 1] = userName; data.SpeItems[7, 1] = email; data.SpeItems[2, 1] = Common.MD5GB2312(userName + data.ApiKey).Substring(8, 0x10); data.PrepareXml(true); data.SendPost(); if (data.FoundErr) { return false; } return true; } public static bool CheckName(string userName, string email) { ApiData data = new ApiData(); data.SpeItems[1, 1] = "checkname"; data.SpeItems[5, 1] = userName; data.SpeItems[7, 1] = email; data.SpeItems[2, 1] = Common.MD5GB2312(userName + data.ApiKey).Substring(8, 0x10); data.PrepareXml(true); data.SendPost(); if (data.FoundErr) { return false; } return true; } public static string DeleteUsers(string userName) { userName = userName.Replace("'", ""); if (string.IsNullOrEmpty(userName)) { return "false"; } ApiData data = new ApiData(); data.SpeItems[1, 1] = "delete"; data.SpeItems[5, 1] = userName; data.SpeItems[2, 1] = Common.MD5GB2312(userName + data.ApiKey).Substring(8, 0x10); data.PrepareXml(true); data.SendPost(); if (data.FoundErr) { return data.ErrMsg; } return "true"; } public static bool LockUser(string userName, int userstatus) { ApiData data = new ApiData(); data.SpeItems[1, 1] = "lock"; data.SpeItems[5, 1] = userName; data.SpeItems[3, 1] = userstatus.ToString(CultureInfo.CurrentCulture); data.SpeItems[2, 1] = Common.MD5GB2312(userName + data.ApiKey).Substring(8, 0x10); data.PrepareXml(true); data.SendPost(); if (data.FoundErr) { return false; } return true; } public static void LogOff(string userName) { ApiData data = new ApiData(); data.SpeItems[5, 1] = userName; data.SpeItems[2, 1] = Common.MD5GB2312(userName + data.ApiKey).Substring(8, 0x10); foreach (string str in data.Urls) { HttpContext.Current.Response.Write("<script type=\"text/javascript\" language=\"JavaScript\" src=\"" + str + "?syskey=" + data.SpeItems[2, 1] + "&username=" + HttpUtility.UrlEncode(userName, Encoding.GetEncoding("GB2312")) + "\"></script>"); } } }
响应
格式:
HTTP/1.1 200 OK Content-Type: text/xml; charset=gb2312 Content-Length: length
<?xml version="1.0" encoding="gb2312"?>
<root>
<appid>程序标识</appid>
<status>操作结果</status>
<body>
<message>操作不成功时返回的错误描述</message>
<可选元素列表 />
</body>
</root>
提供 API 响应的类( Reponse 类)
/// <summary> /// 提供API响应的类 /// </summary> public class Reponse : System.Web.UI.Page { protected ApiData API; // 客户端发送过来的请求数据 private static Hashtable paramsList = new Hashtable(); public Reponse(){ this.API = new ApiData(); this.Load += new EventHandler(Page_Load); } protected void Page_Load(object sender, EventArgs e) { if (this.API.ApiEnable.ToLower() != "true"){ this.API.ErrMsg = "接口没有开通"; this.API.WriteErrXml(); } //syskey username password savecookie paramsList = GetParamsFromRequest(Request); if (paramsList.Count > 0){ this.API.SpeItems[2, 1] = GetRequest(this.API.SpeItems[2, 0]); this.API.SpeItems[5, 1] = GetRequest(this.API.SpeItems[5, 0]); this.API.SpeItems[6, 1] = GetRequest(this.API.SpeItems[6, 0]); this.API.SpeItems[10, 1] = GetRequest(this.API.SpeItems[10, 0]); }else{ NameValueCollection values = HttpUtility.ParseQueryString(base.Request.Url.Query, Encoding.GetEncoding("UTF-8")); this.API.SpeItems[2, 1] = this.Context.Request.QueryString[this.API.SpeItems[2, 0]]; this.API.SpeItems[5, 1] = values[this.API.SpeItems[5, 0]]; this.API.SpeItems[6, 1] = this.Context.Request.QueryString[this.API.SpeItems[6, 0]]; this.API.SpeItems[10, 1] = this.Context.Request.QueryString[this.API.SpeItems[10, 0]]; } Response.AddHeader("P3P", "CP=CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"); if ((this.API.SpeItems[2, 1] != "") && (this.API.SpeItems[5, 1] != null)) { if ((this.API.SpeItems[5, 1] != "") && (this.API.SpeItems[5, 1] != null)) { if ((this.API.SpeItems[6, 1] != "") && (this.API.SpeItems[6, 1] != null)) { this.Loginon(); } else { this.Loginout(); } } } else { this.DealResponse(); } } }