携程分销联盟提供酒店、机票、团购、度假几大旅游产品分销接口;本系列主要研究度假相关接口。

接口文档下载地址:http://open.ctrip.com/help/CommonParams2.aspx?belong=CooperationMode&pagename=CommonParams2

度假接口最主要的两个接口是:

  • PkgProductSearch 产品搜索
  • ProductInfoSearch 产品信息查询

其它接口都是关于度假接口相关基础资料。

-------------------------------------------------------------------------------------------------------------------------------------------

本文首先研究“景区信息”实现接口的例子:

接口说明

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;//联盟IDrequestBody.ProtocolType = 1;//请求类型0-xml  1-JsonrequestBody.Channel = "Vacations";//频道(Vacations-度假)requestBody.Interface = RequestType;//接口名称requestBody.SID = SID;//站点IDrequestBody.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; }}
}

ServiceStack.Text下载地址 https://github.com/ServiceStack/ServiceStack.Text

本文只是一步一步的讲了怎么样实现携程接口,下一节我们介绍下接口功能的封装。

三五旅游网:http://www.35lvyou.com

转载于:https://www.cnblogs.com/caiqin/p/3404422.html

携程分销联盟-旅游度假接口实现1相关推荐

  1. 19岁携程的年轻化旅游 | 一点财经

    "长江后浪推前浪,浮事新人换旧人."在这个世界上没有人能永远年轻,但永远有人正在年轻着. 年轻,也正是企业长青的秘诀. 今年181岁的宝洁.72岁的雅诗兰黛,在自己的财报中都早早地 ...

  2. 去哪儿对垒携程 在线旅游静悄悄的革命

    出处:21世纪经济报道 时间:2011-05-11 10:38[ 字体: 大 中 小 ] [打印此页] [关闭] 颠覆与被颠覆的游戏,正于在线旅游市场悄然演绎.  5月10日,携程收报48.3美元,市 ...

  3. 景点接口 查询携程旅游门票景点详情

    门票景点详情,景点接口支持查询携程旅游门票景点详情. 接口名称:景点接口 接口平台:开放api 接口地址:http://api2.juheapi.com/xiecheng/senicspot/tick ...

  4. 美女导游孙洁到欧洲推销中国旅游,携程缘何成了名片?

    近日,第十二届中欧工商峰会在比利时首都布鲁塞尔召开,多家中国企业随国务院总理李克强参会与欧洲工商界交流合作.携程CEO孙洁尤为引人注目:她是<福布斯>杂志"2017中国最杰出商界 ...

  5. 透过携程财报看在线旅游行业复苏进度,今年旅游行业要找回信心?

    回顾2020年,美股旅游板块经历了跌宕起伏的一年.随着疫苗的出现,华尔街的投资者们对旅游行业又充满信心,很多旅行股出现强劲的反弹. 其中,在线旅游行业仍然是整个旅游板块表现最为强劲的行业.或许是投资者 ...

  6. 分享住宿“大混战”:Airbnb、途家、携程、美团、Priceline,这些旅行巨头都来了

    本文由全天候科技(ID:iawtmt)授权转载.全天候科技是华尔街见闻发起的原创科技新媒体. 中国分享住宿市场,外资巨头Airbnb.以途家为代表的分享住宿平台.以美团榛果为代表的后起之秀正在 &qu ...

  7. 携程发布2021年一季度财报:净利润环比增长近80% 达18亿元人民币

    北京时间2021年5月19日, 携程集团(纳斯达克:TCOM及香港联交所:9961)公布了截至2021年3月31日第一季度未经审计的财务业绩. 在全球旅游业饱受疫情冲击,国内疫情防控政策在年初收紧的背 ...

  8. 携程发布2020年财报:四季度营业利润率10% 疫情以来连续两季度盈利

    北京时间2021年3月4日, 携程集团(纳斯达克股票代码:TCOM)公布了截至2020年12月31日第四季度及全年的财务业绩. 在全球旅游业饱受疫情冲击的背景下,携程在2020年全年的业绩表现&quo ...

  9. 携程签约日本爱知县 探索主题游促中日交流

    8月26日,名古屋,携程旅行网与日本爱知县宣布正式达成战略合作.双方将在目的地营销.旅游产品推广等多个领域深入合作,爱知县也将为携程客人开放更多特色旅游资源.爱知县知事大村秀章.携程旅行网CMO孙波等 ...

  10. Q1营收超预期但仍呈负增长,携程究竟在哪里“丢了分”?

    继同程艺龙之后,国内"OTA巨头"携程也于5月19日对外发布了新一季财报,这也是其回港后的首份成绩单. 财报显示,携程集团营收的下降幅度已经缩窄至13%,营收体量超出市场预期:同时 ...

最新文章

  1. 1.mongodb在centos上面安装
  2. oracle 相同的sql执行两次 执行计划会不一样吗,一条SQL语句,两次执行计划的差距...
  3. 用C#实现计算机图形学算法
  4. Linux 文件基本属性
  5. Windows x64内核学习笔记(五)—— KPTI(未完待续)
  6. C++中结构体、联合体、枚举的区别
  7. Redis—主从复制
  8. 【Java网络编程(三)】TCP的使用——模拟用户登录
  9. mount 返回状态_状态管理模式 — Vuex如何使用?
  10. Android Studio中解决jar包重复依赖导致的代码编译错误
  11. iap 审核 文档_为什么必须审核文档
  12. elupload获取文件名与路径_Python检查word文件中的特殊“标记”词是否与文件名中的一致(实例59)...
  13. 使用getApplication()作为上下文的对话框抛出“无法添加窗口-令牌null不适用于应用程序”
  14. 开发了一个拼多多淘宝闲鱼所有虚拟店商品通过百度网盘自动发货机器人软件助手
  15. 自底向上和自顶向下的区别
  16. 计算机系统无法启动 错误恢复怎么办,windows7恢复错误,无法进入系统最佳解决方法...
  17. 我想不通,MySQL 为什么使用 B+ 树来作索引?
  18. 283页K8S实战指南,内容详实,代码齐全可复制!
  19. 关于switch-case的用法细节及其特殊用法
  20. 趣谈implicit instantiation of undefined template

热门文章

  1. 无论如何,你该在大城市再坚持下
  2. BAT架构师推荐的9本程序员技术进阶图书,大家看过多少?
  3. file图片上传之前先预览
  4. 亚马逊云平台采集转单机采集实现
  5. hdoj1116【欧拉回路】
  6. 第三百四十五天 how can I 坚持
  7. iOS Your account already has a valid ios Distribution certificate
  8. emule学习与分析二 上 建立连接过程分析
  9. WebService—实现接口发布和客户端调用的几种方式
  10. django后台管理--添加自定义action