using Newtonsoft.Json; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Net.Http; using System.Net.Sockets; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; namespace GuangFu.StringTracing.Common { public class CtripApi { #region 获取携程Ticket public string GetCtripTicket() { string path = "https://ct.ctrip.com/SwitchAPI/Order/Ticket"; string result = string.Empty; var body = @"{ " + "\n" + @" ""appKey"": ""obk_GXHJ"", " + "\n" + @" ""appSecurity"": ""mD2!^w7CCGq)U%c*An*um%^T"" " + "\n" + @"}"; HttpContent httpContent = new StringContent(body); //设置Http的内容标头 httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); //设置Http的内容标头的字符 httpContent.Headers.ContentType.CharSet = "utf-8"; using (HttpClient httpClient = new HttpClient()) { //异步Post HttpResponseMessage response = httpClient.PostAsync(path, httpContent).Result; //确保Http响应成功 if (response.IsSuccessStatusCode) { //异步读取json result = response.Content.ReadAsStringAsync().Result; CtripRoot tokenData = JsonConvert.DeserializeObject(result); return tokenData.Ticket; } else { return ""; } } } #endregion #region 获取携程全量国家数据 public string GetXCCountry(string Ticket) { string path = "https://ct.ctrip.com/switchAPI/basedata/v2/getcountry"; string result = string.Empty; HttpContent httpContent = new StringContent("{\r\n \"Auth\": {\r\n \"AppKey\": \"obk_GXHJ\",\r\n \"Ticket\": \""+ Ticket + "\"\r\n },\r\n \"locale\": \"zh-CN\"\r\n}"); //设置Http的内容标头 httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); //设置Http的内容标头的字符 httpContent.Headers.ContentType.CharSet = "utf-8"; using (HttpClient httpClient = new HttpClient()) { //异步Post HttpResponseMessage response = httpClient.PostAsync(path, httpContent).Result; //确保Http响应成功 if (response.IsSuccessStatusCode) { //异步读取json result = response.Content.ReadAsStringAsync().Result; } } return result; } #endregion #region 获取携程国内城市数据 public string GetXCChinaCity(string Ticket) { string path = "https://ct.ctrip.com/switchapi/basedata/v2/queryAllPOIInfo"; string result = string.Empty; HttpContent httpContent = new StringContent("{\r\n \"auth\": {\r\n \"AppKey\": \"obk_GXHJ\",\r\n \"Ticket\": \""+ Ticket + "\"\r\n },\r\n \"provinceConditions\": {\r\n \"prefectureLevelCityConditions\": {\r\n \"returnDistrict\": false\r\n }\r\n },\r\n \"countryId\": 1,\r\n \"poiConditions\": {\r\n \"returnAirport\": false,\r\n \"returnTrainStation\": false,\r\n \"returnBusStation\": false\r\n }\r\n}"); //设置Http的内容标头 httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); //设置Http的内容标头的字符 httpContent.Headers.ContentType.CharSet = "utf-8"; using (HttpClient httpClient = new HttpClient()) { //异步Post HttpResponseMessage response = httpClient.PostAsync(path, httpContent).Result; //确保Http响应成功 if (response.IsSuccessStatusCode) { //异步读取json result = response.Content.ReadAsStringAsync().Result; } } return result; } #endregion #region 提前审批接口 public static string PostData(string postString) { byte[] postData = Encoding.UTF8.GetBytes(postString); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri("https://ct.ctrip.com/switchapi/approval/save")); request.Method = "POST"; request.ServicePoint.Expect100Continue = false; request.ContentType = "application/json"; request.ContentLength = postData.Length; using (Stream requestStream = request.GetRequestStream()) { requestStream.Write(postData, 0, postData.Length); } HttpWebResponse response = (HttpWebResponse)request.GetResponse(); StreamReader stream = new StreamReader(response.GetResponseStream(), Encoding.UTF8); string srcString = stream.ReadToEnd(); response.Close(); stream.Close(); return srcString; } #endregion #region 预算数据落地 public static string SaveBudget(string postString) { byte[] postData = Encoding.UTF8.GetBytes(postString); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri("https://corpsz.ctrip.com/PreApproval/saveBudget")); request.Method = "POST"; request.ServicePoint.Expect100Continue = false; request.ContentType = "application/json"; request.ContentLength = postData.Length; using (Stream requestStream = request.GetRequestStream()) { requestStream.Write(postData, 0, postData.Length); } HttpWebResponse response = (HttpWebResponse)request.GetResponse(); StreamReader stream = new StreamReader(response.GetResponseStream(), Encoding.UTF8); string srcString = stream.ReadToEnd(); response.Close(); stream.Close(); return srcString; } #endregion #region 预算落地接口加密方法 /// /// 预算落地接口加密方法 /// /// 审批单号 /// 加密密钥 /// 预算信息列表 /// 加密结果 public string CreateSign(string approvalNumber, string appSecurity, List budgetInfoList) { if (approvalNumber == null || appSecurity == null || budgetInfoList == null || budgetInfoList.Count == 0) { throw new ArgumentException("加密参数异常"); } // 按 Products 排序 budgetInfoList.Sort((o1, o2) => o1.Products.CompareTo(o2.Products)); // 拼接预算字符串 var budgetStr = new StringBuilder(); foreach (var budgetInfo in budgetInfoList) { budgetStr.Append(budgetInfo.Products).Append(budgetInfo.BudgetAmount); } // 加密 string plainText = approvalNumber + budgetStr + Sha1Hex(appSecurity); return Sha1Hex(plainText); } private static string Sha1Hex(string input) { using (var sha1 = SHA1.Create()) { byte[] bytes = sha1.ComputeHash(Encoding.UTF8.GetBytes(input)); return BitConverter.ToString(bytes).Replace("-", "").ToLower(); } } #endregion } #region 自定义变量 public class Status { /// /// /// public bool Success { get; set; } /// /// /// public int ErrorCode { get; set; } /// /// 调用成功。 /// public string Message { get; set; } } public class CtripRoot { /// /// /// public Status Status { get; set; } /// /// /// public string Ticket { get; set; } } /// /// 携程接口请求模型 /// public class CtripApprovalRequest { /// /// 申请单号 /// public string ApprovalNumber { get; set; } /// /// 状态:固定值1(有效) /// public int Status { get; set; } = 1; /// /// 员工工号 /// public string EmployeeID { get; set; } /// /// 过期时间 /// public DateTime ExpiredTime { get; set; } /// /// 备注 /// public string Remark { get; set; } /// /// 鉴权对象 /// public CtripAuth Auth { get; set; } /// /// 扩展字段列表 /// public List ExtendFieldList { get; set; } /// /// 机票行程明细列表 /// public List FlightEndorsementDetails { get; set; } /// /// 酒店行程明细列表 /// public List HotelEndorsementDetails { get; set; } } /// /// 机票行程明细 /// public class FlightEndorsementDetail { /// /// 产品类型:固定FLIGHT /// public string ProductType { get; set; } = "FLIGHT"; ///// ///// 行程类型:1-单程,2-往返 ///// public int FlightWay { get; set; } ///// ///// 出发开始日期 ///// //public DateTime DepartDateBegin { get; set; } ///// ///// 出发结束日期 ///// //public DateTime DepartDateEnd { get; set; } ///// ///// 返程开始日期 ///// //public DateTime? ReturnDateBegin { get; set; } ///// ///// 返程结束日期 ///// //public DateTime? ReturnDateEnd { get; set; } ///// ///// 出发城市编码列表 ///// //public List DepartCityIds { get; set; } ///// ///// 到达城市编码列表 ///// //public List ArrivalCityIds { get; set; } ///// ///// 舱位等级:3-经济舱 ///// //public int SeatClass { get; set; } = 3; /// /// 出行人列表 /// public List PassengerList { get; set; } } /// /// 酒店行程明细 /// public class HotelEndorsementDetail { /// /// 产品类型:固定HOTEL /// public string ProductType { get; set; } = "HOTEL"; ///// ///// 入住开始日期 ///// //public DateTime CheckInDateBegin { get; set; } ///// ///// 入住结束日期 ///// //public DateTime CheckInDateEnd { get; set; } ///// ///// 离店开始日期 ///// //public DateTime CheckOutDateBegin { get; set; } ///// ///// 离店结束日期 ///// //public DateTime CheckOutDateEnd { get; set; } ///// ///// 入住城市编码列表 ///// //public List CheckInCityCodes { get; set; } /// /// 出行人列表 /// public List PassengerList { get; set; } } /// /// 出行人信息 /// public class PassengerInfo { /// /// 员工工号 /// public string EID { get; set; } /// /// 英文姓名(暂不传) /// public string NameEn { get; set; } } /// /// 携程鉴权对象 /// public class CtripAuth { /// /// 应用Key /// public string AppKey { get; set; } = "obk_GXHJ"; /// /// 动态票据 /// public string Ticket { get; set; } } /// /// 携程扩展字段 /// public class CtripExtendField { /// /// 字段名 /// public string FieldName { get; set; } /// /// 字段值 /// public string FieldValue { get; set; } } public class CtripBudgetRequest { /// /// 申请单号 /// public string ApprovalNumber { get; set; } /// /// 签名 /// public string Signature { get; set; } /// /// 鉴权对象 /// public CtripAuth Auth { get; set; } /// /// 扩展字段列表 /// public List BudgetInfoList { get; set; } } public class BudgetInfo { public decimal BudgetAmount { get; set; } public long Products { get; set; } = 12; } #endregion }