public static ResponseT send <RequestT, ResponseT>(RequestT reqObj, CommunicationInformation commInfo, uint tuid = 0, bool replaceFlag = false) { if (reqObj == null) { throw new ArgumentNullException("reqObj is null."); } if (commInfo == null) { throw new ArgumentNullException("commInfo is null."); } MemoryStream msReq = null; MemoryStream msRsp = null; // for Sprunk to show message with if (NeedPrintMessageToConsole) { Console.WriteLine("Run Test case on Server :" + commInfo.URI); Console.Error.WriteLine("Now Run Test case on Server :" + commInfo.URI); Console.WriteLine("-----------Request:-----------"); Print.PrintMessageToConsole(reqObj); } // Serialize Request [obj] to [objXML] msReq = XMLSerializer.Serialize(reqObj, typeof(RequestT)); // Attach Protocol [objXML] to [Hdr + objXML] msReq = MessageProtocol.AttachProtocol(msReq, commInfo); // Serialize Request [Hdr + objXML] to [FI[Hdr + objXML]] msReq = MessageFormat.Serialize(msReq, commInfo.ContentType); // Encode Request [FI[Hdr + objXML]] to [GZip[FI[Hdr + objXML]]] msReq = MessageEncodeDecode.Encode(msReq, commInfo.ContentEncoding.ToString()); // Send/Recv Request/Response if (NeedPrintMessageToConsole) { Console.WriteLine(string.Format("-----------Begin to send request to service, transport encoding is {0}: -----------", commInfo.ContentType.ToString())); } // It's just for spoofer if (Convert.ToBoolean(CarConfigurationManager.AppSetting("NeedSpoofer")) == true) { if (tuid > 0) { commInfo.TestCaseID = tuid; if (NeedPrintMessageToConsole) { Console.WriteLine("TUID from parameter:" + tuid + "!"); } } else { commInfo.TestCaseID = CarCommonRequestGenerator.getTUIDFromRequest(reqObj); { if (commInfo.TestCaseID == 0) { Console.WriteLine("TUID can't be get from request object!"); } else { Console.WriteLine("TUID from request object:" + commInfo.TestCaseID + "!"); } } } } else { if (NeedPrintMessageToConsole) { Console.WriteLine("Spoofer is off, no testcaseID get!"); } } ResponseT rspobj = default(ResponseT); string statusCode = ""; HttpWebResponse httpWebResponse = SendReqAndRecvResp(msReq, commInfo, typeof(RequestT).Name); if (null != httpWebResponse) { statusCode = httpWebResponse.StatusCode.ToString(); } MemoryStream msRspReturn = CommunicationUtil.StreamToMemoryStream(httpWebResponse.GetResponseStream()); if (msRspReturn == null || msRspReturn.Length == 0) { throw new Exception("Response is empty or Null, please check."); } else { // Decode Response [GZip[FI[Hdr + objXML]]] to [FI[Hdr + objXML]] msRspReturn = MessageEncodeDecode.Decode(msRspReturn, httpWebResponse.ContentEncoding); // DeSerialize Response [FI[Hdr + objXML]] to [Hdr + objXML] msRspReturn = MessageFormat.Deserialize(msRspReturn, commInfo.ContentType); // Detach Protocol [Hdr + objXML] to [objXML] msRspReturn = MessageProtocol.DetachProtocol(msRspReturn, commInfo.MessageProtocol); msRsp = msRspReturn; // handle aws namespace issue if (replaceFlag) { string siteName = ServiceConfigUtil.CarProductTokenUriGet(); if (siteName.Contains("cars-business-service")) { string responseStr = System.Text.Encoding.GetEncoding(httpWebResponse.CharacterSet).GetString(msRspReturn.ToArray()); responseStr = responseStr.Replace("urn:com", "urn"); XmlDocument rspXml = new XmlDocument(); rspXml.LoadXml(responseStr); msRsp = CommunicationUtil.ConvertXmlDocumentToMemoryStream(rspXml); } } // Deserialize Response [objXML] to [obj] rspobj = (ResponseT)XMLSerializer.DeSerialize(msRsp, typeof(ResponseT)); if (NeedPrintMessageToConsole) { Console.WriteLine("-----------Response:-----------"); Print.PrintMessageToConsole(rspobj); } } return(rspobj); }
public static Double GetCurrencyConversionRateSend(String baseCurCode, String targetCurCode) { if (baseCurCode == null) { throw new ArgumentNullException("baseCurCode"); } if (targetCurCode == null) { throw new ArgumentNullException("targetCurCode"); } Double exchangeRate = -1.00; // error value XmlDocument docRequest = new XmlDocument(); string xmlStr = "" + //"<?xml version='1.0' encoding='utf-8'?>\r\n" + "<s:Envelope " + "xmlns:s='http://www.w3.org/2003/05/soap-envelope' " + "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' " + "xmlns:xsd='http://www.w3.org/2001/XMLSchema'>\r\n" + "</s:Envelope>"; try { docRequest.LoadXml(xmlStr); //Get root element XmlElement envelope = docRequest.DocumentElement; // Create body XmlElement body = docRequest.CreateElement("s", "Body", "http://www.w3.org/2003/05/soap-envelope"); body.InnerXml = FxRatesRequestTemplate(); envelope.AppendChild(body); // Set DateTime String currentDate = DateTime.Now.ToString("o"); XmlNode createDateTime = docRequest.GetElementsByTagName("urn:CreateDateTime")[0]; createDateTime.InnerXml = currentDate; // Set BaseCurrencyCode XmlNode baseCurrencyCode = docRequest.GetElementsByTagName("urn:BaseCurrencyCode")[0]; baseCurrencyCode.InnerXml = baseCurCode; // Set TargetCurrencyCode XmlNode targetCurrencyCode = docRequest.GetElementsByTagName("urn:TargetCurrencyCode")[0]; targetCurrencyCode.InnerXml = targetCurCode; // Send/Recv String fxRsUri = CarConfigurationManager.AppSetting("FXRSUri"); CommunicationInformation commInfo = new CommunicationInformation(fxRsUri, MsgProtocol.SOAP, MessageFormat.MessageContentType.Xml, MessageEncodeDecode.MessageContentEncoding.gzip, false); HttpWebResponse rsp = CommunicationManager.SendReqAndRecvResp(CommunicationUtil.ConvertXmlDocumentToMemoryStream(docRequest), commInfo); MemoryStream msRsp = CommunicationUtil.StreamToMemoryStream(rsp.GetResponseStream()); msRsp.Seek(0, SeekOrigin.Begin); // Load Response in doc XmlDocument docResponse = new XmlDocument(); docResponse.Load(msRsp); // Extract exchange rate XmlNode rate = null; if (docResponse.GetElementsByTagName("Rate", "urn:expedia:xmlapi:fxrs:v1").Count == 0) { return(exchangeRate); } rate = docResponse.GetElementsByTagName("Rate", "urn:expedia:xmlapi:fxrs:v1")[0]; exchangeRate = Convert.ToDouble(rate.InnerXml); } catch (Exception e) { Console.WriteLine(string.Format("Error when sending FXRS for baseCurCode:{0}, targetCurCode: {1}. ", baseCurCode, targetCurCode)); //throw e; } return(exchangeRate); }