携程分销联盟提供酒店、机票、团购、度假几大旅游产品分销接口;本系列主要研究度假相关接口。
接口文档下载地址:http://open.ctrip.com/help/CommonParams2.aspx?belong=CooperationMode&pagename=CommonParams2
度假接口最主要的两个接口是:
其它接口都是关于度假接口相关基础资料。
-------------------------------------------------------------------------------------------------------------------------------------------
本文首先研究“景区信息”实现接口的例子:
接口说明
API_Url
API服务的域名地址
联系商务经理获取(http://openapi.ctrip.com)
AllianceID
分销商ID
可以从u.ctrip.com上注册后获取
SID
站点ID
可以从u.ctrip.com上注册后获取
TimeStamp
从1970年到现在的秒数
RequestType
请求接口的类型
Signature
MD5加密串
可以通过《技术资料》获取帮助支持
接口地址
http://openapi.ctrip.com/vacations/OpenServer.ashx
调用方式
参数:RequestJson=UrlEncode(请求报文)
例:
http://openapi.ctrip.com/vacations/OpenServer.ashx?RequestJson=%7b%22AllianceID%22%3a%221%22%2c%22SID%22%3a%221%22%2c%22ProtocolType%22%3a1%2c%22Signature%22%3a%22817D9FF70999176DE1F887D5CC4B732B%22%2c%22TimeStamp%22%3a%221371177110%22%2c%22Channel%22%3a%22Vacations%22%2c%22Interface%22%3a%22AddressSelectorInfoSearch%22%2c%22IsError%22%3afalse%2c%22RequestBody%22%3a%22%7b%5c%22AddressSelectorIDs%5c%22%3a%5b1%2c2%5d%7d%22%2c%22ResponseBody%22%3a%22%22%2c%22ErrorMessage%22%3a%22%22%7d
请求报文格式
报文最外层使用json格式,字段RequestBody中根据不同需要,使用XML或JSON。
RequestJson格式统一为:
{
"AllianceID": "1",
"SID": "1",
"ProtocolType": 1,
"Signature": "9CDA085BE3957CB5D5CE866BFA25A07D",
"TimeStamp": "1371177060",
"Channel": "Vacations",
"Interface": "AddressSelectorInfoSearch",
"IsError": false,
"RequestBody": "输入参数内容",
"ResponseBody": "",
"ErrorMessage": ""
}
HTTP返回格式统一为:
{
"AllianceID": 4802,
"SID": 105454,
"ProtocolType": 1,
"Signature": "2AEC78CB92AE382E016D307B8BCD8787",
"TimeStamp": "1383400366",
"Channel": "Vacations",
"Interface": "DistrictSearch",
"IsError": false,
"RequestBody": "",
"Header": {
"Culture": "",
"ShouldRecordPerformanceTime": false,
"Timestamp": "2013-11-02 21:52:39:90826",
"ReferenceID": "6515e738-1109-44cf-b00e-15e9f87a877d",
"ResultCode": 0,
"ResultNo": null,
"ResultMsg": "",
"RecentlyTime": "2013-11-02 21:52:38",
"AccessCount": 3000,
"CurrentCount": 1,
"ResetTime": "2013-11-02 21:53:38"
},
"ResponseBody": "返回内容",
"ErrorMessage": ""
}
备注:
顺便给大家推荐一个json格式化校验的网址:http://www.bejson.com/go.php?u=http://www.bejson.com/index.php
接口的输入输出参数都是以json格式进行传递,我使用的json和Entity序列化的工具是:ServiceStack.Text
---------------------------------------------------------------------------------------------------------------------------------------
1、Signature算法
/// <summary> /// 签名算法 /// </summary> /// <param name="SecretKey">密钥:站点的APIKey,可以在“站点列表”中查到;</param> /// <param name="AllianceID">联盟代码,可以在“我的账户”中查到;</param> /// <param name="SID">联盟的站点ID,可以在“站点列表”中查到;</param> /// <param name="RequestType">请求接口服务的名称</param> /// <param name="RequestType">从1970年到现在的秒数</param> /// <returns></returns> public string GetSignature(string SecretKey, int AllianceID, int SID, string RequestType, string TimeStamp) { string md5 = FormsAuthentication.HashPasswordForStoringInConfigFile(SecretKey, "MD5").ToUpper(); return FormsAuthentication.HashPasswordForStoringInConfigFile(TimeStamp + AllianceID + md5 + SID + RequestType, "MD5"); }
2、HttpPOST取得数据
/// <summary> /// HttpPost取得数据 /// </summary> /// <param name="url">网址</param> /// <param name="data">参数</param> /// <returns></returns> private string GetHttpPostX(string url, Dictionary<string, string> data) { try { WebClient WC = new WebClient(); WC.Encoding = System.Text.Encoding.UTF8; System.Collections.Specialized.NameValueCollection Col = new System.Collections.Specialized.NameValueCollection(); foreach (KeyValuePair<string, string> item in data) { Col.Add(item.Key, item.Value); } byte[] responseArray = WC.UploadValues(url, "POST", Col); string response = Encoding.UTF8.GetString(responseArray); return response; } catch (Exception Ex) { return Ex.Message; } }
3、接口请求返回参数实体
public class APICallEntity { public int AllianceID { set; get; } public int SID { set; get; } public int ProtocolType { set; get; } public string Signature { set; get; } public string TimeStamp { set; get; } public string Channel { set; get; } public string Interface { set; get; } public bool IsError { set; get; } public string RequestBody { set; get; } public HeaderEntity Header { set; get; } public string ResponseBody { set; get; } public string ErrorMessage { set; get; } } public class HeaderEntity { public string Culture { set; get; } public string ShouldRecordPerformanceTime { set; get; } public DateTime Timestamp { set; get; } public string ReferenceID { set; get; } }
3、实现接口:DistrictSearch 景区ID查询
请求参数说明:
QueryCount int 查询数量
StartID int 起始ID
返回参数说明:
DistrictIDs List<int> 景区信息列表
请求参数和返回参数实体
public class DistrictSearchCallEntity { public int QueryCount { set; get; } public int StartID { set; get; } } public class DistrictSearchCallReturnEntity { public List<int> DistrictIDs { set; get; } }
具体实现方法
string url = "http://openapi.ctrip.com/vacations/OpenServer.ashx"; string RequestType = "DistrictSearch"; string SecretKey = "74692D43-FA20-4FFB-B537-9A99D16AC71D"; int AllianceID = 4802; int SID = 105454; //计算从1970年到现在的秒数; TimeSpan span = (TimeSpan)(DateTime.UtcNow - new DateTime(0x7b2, 1, 1, 0, 0, 0, 0)); string TimeStamp = Convert.ToInt64(span.TotalSeconds).ToString(); //请求参数 DistrictSearchCallEntity callentity = new DistrictSearchCallEntity() { QueryCount = 10, StartID = 1 }; APICallEntity requestBody = new APICallEntity(); requestBody.AllianceID = AllianceID;//联盟ID requestBody.ProtocolType = 1;//请求类型0-xml 1-Json requestBody.Channel = "Vacations";//频道(Vacations-度假) requestBody.Interface = RequestType;//接口名称 requestBody.SID = SID;//站点ID requestBody.TimeStamp = TimeStamp;//1970年到现在的秒数 requestBody.Signature = GetSignature(SecretKey, AllianceID, SID, RequestType, TimeStamp);//签名 requestBody.RequestBody = callentity.ToJson();//请求查询参数,根据ProtocolType使用不同格式(ServiceStack.Text) Dictionary<string, string> data = new Dictionary<string, string>(); data.Add("RequestJson", requestBody.ToJson());//格式化请求参数内容(ServiceStack.Text) string result = GetHttpPostX(url, data); //格式化返回内容(ServiceStack.Text) var rerurnEntiey = result.FromJson<APICallEntity>(); //判断实现接口是否成功 if (!rerurnEntiey.IsError) { //格式化返回内容(ServiceStack.Text) var callreturnEntity = rerurnEntiey.ResponseBody.FromJson<DistrictSearchCallReturnEntity>(); }
本节全部源码:
using System; using System.Collections.Generic; using System.Web.Security; using System.Net; using System.Text; using ServiceStack.Text; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string url = "http://openapi.ctrip.com/vacations/OpenServer.ashx"; string RequestType = "DistrictSearch"; string SecretKey = "74692D43-FA20-4FFB-B537-9A99D16AC71D"; int AllianceID = 4802; int SID = 105454; //计算从1970年到现在的秒数; TimeSpan span = (TimeSpan)(DateTime.UtcNow - new DateTime(0x7b2, 1, 1, 0, 0, 0, 0)); string TimeStamp = Convert.ToInt64(span.TotalSeconds).ToString(); //请求参数 DistrictSearchCallEntity callentity = new DistrictSearchCallEntity() { QueryCount = 10, StartID = 1 }; APICallEntity requestBody = new APICallEntity(); requestBody.AllianceID = AllianceID;//联盟ID requestBody.ProtocolType = 1;//请求类型0-xml 1-Json requestBody.Channel = "Vacations";//频道(Vacations-度假) requestBody.Interface = RequestType;//接口名称 requestBody.SID = SID;//站点ID requestBody.TimeStamp = TimeStamp;//1970年到现在的秒数 requestBody.Signature = GetSignature(SecretKey, AllianceID, SID, RequestType, TimeStamp);//签名 requestBody.RequestBody = callentity.ToJson();//请求查询参数,根据ProtocolType使用不同格式(ServiceStack.Text) Dictionary<string, string> data = new Dictionary<string, string>(); data.Add("RequestJson", requestBody.ToJson());//格式化请求参数内容(ServiceStack.Text) string result = GetHttpPostX(url, data); //格式化返回内容(ServiceStack.Text) var rerurnEntiey = result.FromJson<APICallEntity>(); //判断实现接口是否成功 if (!rerurnEntiey.IsError) { //格式化返回内容(ServiceStack.Text) var callreturnEntity = rerurnEntiey.ResponseBody.FromJson<DistrictSearchCallReturnEntity>(); } } /// <summary> /// 签名算法 /// </summary> /// <param name="SecretKey">密钥:站点的APIKey,可以在“站点列表”中查到;</param> /// <param name="AllianceID">联盟代码,可以在“我的账户”中查到;</param> /// <param name="SID">联盟的站点ID,可以在“站点列表”中查到;</param> /// <param name="RequestType">请求接口服务的名称</param> /// <param name="RequestType">从1970年到现在的秒数</param> /// <returns></returns> public string GetSignature(string SecretKey, int AllianceID, int SID, string RequestType, string TimeStamp) { string md5 = FormsAuthentication.HashPasswordForStoringInConfigFile(SecretKey, "MD5").ToUpper(); return FormsAuthentication.HashPasswordForStoringInConfigFile(TimeStamp + AllianceID + md5 + SID + RequestType, "MD5"); } /// <summary> /// HttpPost取得数据 /// </summary> /// <param name="url">网址</param> /// <param name="data">参数</param> /// <returns></returns> private string GetHttpPostX(string url, Dictionary<string, string> data) { try { WebClient WC = new WebClient(); WC.Encoding = System.Text.Encoding.UTF8; System.Collections.Specialized.NameValueCollection Col = new System.Collections.Specialized.NameValueCollection(); foreach (KeyValuePair<string, string> item in data) { Col.Add(item.Key, item.Value); } byte[] responseArray = WC.UploadValues(url, "POST", Col); string response = Encoding.UTF8.GetString(responseArray); return response; } catch (Exception Ex) { return Ex.Message; } } public class APICallEntity { public int AllianceID { set; get; } public int SID { set; get; } public int ProtocolType { set; get; } public string Signature { set; get; } public string TimeStamp { set; get; } public string Channel { set; get; } public string Interface { set; get; } public bool IsError { set; get; } public string RequestBody { set; get; } public HeaderEntity Header { set; get; } public string ResponseBody { set; get; } public string ErrorMessage { set; get; } } public class HeaderEntity { public string Culture { set; get; } public string ShouldRecordPerformanceTime { set; get; } public DateTime Timestamp { set; get; } public string ReferenceID { set; get; } } public class DistrictSearchCallEntity { public int QueryCount { set; get; } public int StartID { set; get; } } public class DistrictSearchCallReturnEntity { public List<int> DistrictIDs { set; get; } } }
本文只是一步一步的讲了怎么样实现携程接口,下一节我们介绍下接口功能的封装。
三五旅游网:http://www.35lvyou.com