09年做过GTA的酒店项目,成功地实现了全套业务流程。
由于某种不能透露的原因,现在又要做这件事。翻出了当年的代码,其实很简单,不知道为什么现在的同事对着Document唯一能做的事情就是发呆......
GTA 酒店接口是通过POST方法传递一个XML来实现数据交互的,现把SearchHotelPriceRequest的实现代码粘贴出来,希望能帮到后来人。
稍后会张贴出ItemInformationDownloadRequest的代码......
public System.Xml.XmlDocument SendRequestXmlData() { string EmailAddress = "GTA授权邮件地址"; string ClientPassword = "密码"; string ClientID = "客户号"; string Language = "语言代码"; XmlElement XMLRequest, XMLHeader, XMLNode, XMLChildNode, XMLBody, XMLGrandChild; XmlDocument XMLDoc; string strCheckInDate = DateTime.Now.AddDays(20).ToShortDateString();
XMLDoc = new XmlDocument(); XMLRequest = XMLDoc.CreateElement("Request"); XMLHeader = XMLDoc.CreateElement("Source");
XMLNode = XMLDoc.CreateElement("RequestorID"); XMLNode.SetAttribute("Client", ClientID); XMLNode.SetAttribute("EMailAddress", EmailAddress); XMLNode.SetAttribute("Password", ClientPassword); XMLHeader.AppendChild(XMLNode);
XMLNode = XMLDoc.CreateElement("RequestorPreferences"); XMLNode.SetAttribute("Language", Language); XMLChildNode = XMLDoc.CreateElement("RequestMode"); XMLChildNode.InnerText = "SYNCHRONOUS"; XMLNode.AppendChild(XMLChildNode); XMLHeader.AppendChild(XMLNode);
XMLBody = XMLDoc.CreateElement("RequestDetails"); XMLNode = XMLDoc.CreateElement("SearchHotelPriceRequest"); XMLChildNode = XMLDoc.CreateElement("ItemDestination"); XMLChildNode.SetAttribute("DestinationType", "city"); XMLChildNode.SetAttribute("DestinationCode", "CAN"); XMLNode.AppendChild(XMLChildNode); XMLChildNode = XMLDoc.CreateElement("PeriodOfStay"); XMLGrandChild = XMLDoc.CreateElement("CheckInDate"); XMLGrandChild.InnerText = strCheckInDate; XMLChildNode.AppendChild(XMLGrandChild); XMLGrandChild = XMLDoc.CreateElement("Duration"); XMLGrandChild.InnerText = "2"; XMLChildNode.AppendChild(XMLGrandChild); XMLNode.AppendChild(XMLChildNode); XMLChildNode = XMLDoc.CreateElement("Rooms"); XMLGrandChild = XMLDoc.CreateElement("Room"); XMLGrandChild.SetAttribute("Code", "db"); XMLGrandChild.SetAttribute("NumberOfRooms", "1"); XMLChildNode.AppendChild(XMLGrandChild); XMLNode.AppendChild(XMLChildNode); XMLBody.AppendChild(XMLNode);
XMLRequest.AppendChild(XMLHeader); XMLRequest.AppendChild(XMLBody); XMLDoc.AppendChild(XMLRequest);
System.Xml.XmlDocument XMLDoc1 = new System.Xml.XmlDocument(); System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding(); Byte[] byte1 = encoding.GetBytes(XMLDoc.OuterXml); WebRequest HttpWReq = WebRequest.Create("https://rbs.gta-travel.com/rbshkapi/RequestListenerServlet"); HttpWReq.ContentType = "text/xml"; HttpWReq.ContentLength = XMLDoc.OuterXml.Length; HttpWReq.Method = "POST"; HttpWReq.Timeout = 1200000; System.IO.Stream StreamData = HttpWReq.GetRequestStream(); StreamData.Write(byte1, 0, byte1.Length); WebResponse HttpWRes = HttpWReq.GetResponse(); System.IO.Stream receiveStream = HttpWRes.GetResponseStream(); XMLDoc1.Load(receiveStream); StreamData.Close();
XmlNodeList nodeList = XMLDoc1.GetElementsByTagName("Error"); if (nodeList.Count > 0) { string errorId = nodeList[0].SelectSingleNode("ErrorId").InnerText; string errorText = nodeList[0].SelectSingleNode("ErrorText").InnerText; throw new Exception("错误ID=" + errorId + ",错误内容=" + errorText); } return XMLDoc1; }