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 string FormatRequest(MemoryStream ms, MessageFormat.MessageContentType messageContentType = MessageFormat.MessageContentType.Xml, MessageMode messageMode = MessageMode.Serialize) * { * string strRequest = ""; * MemoryStream outMS; * if (messageMode == MessageMode.Serialize) * { * outMS = MessageFormat.Serialize(ms, messageContentType); * } * else * { * outMS = MessageFormat.Deserialize(ms, messageContentType); * } * * return strRequest; * } * * public static WebRequest getWebRequest(string strRequest, string uri, MessageFormat.MessageContentType messageContentType = MessageFormat.MessageContentType.Xml, * MsgProtocol msgProtocol = MsgProtocol.Interop, bool withE3JRequestHeader = true) * { * WebRequest webRequest = WebRequest.Create(uri); * byte[] byteArray = Encoding.UTF8.GetBytes(strRequest); * webRequest.Method = "POST"; * webRequest.ContentType = getContextType(messageContentType, msgProtocol); * webRequest.ContentLength = byteArray.Length; * * * if (withE3JRequestHeader) * { * // E3JMS-L-origServerName * webRequest.Headers.Add("E3JMS-L-origServerName", Environment.MachineName); * * // E3JMS-L-serviceName * webRequest.Headers.Add("E3JMS-L-serviceName", System.Diagnostics.Process.GetCurrentProcess().ProcessName); * * // e3jms-l-stand-rep * webRequest.Headers.Add("e3jms-l-stand-rep", "TQ:e3Interop"); * * // E3JMS-L-activityId * Guid guid = Guid.NewGuid(); * webRequest.Headers.Add("e3jms-l-activityId-propname", "activityId"); * webRequest.Headers.Add("E3JMS-L-activityId", Convert.ToString(guid)); * * // E3JMS-L-stand-mes * String messageId = Convert.ToString(guid) + "-" + Guid.NewGuid(); * webRequest.Headers.Add("E3JMS-L-stand-mes", messageId); * } * * // Get the request stream. * Stream dataStream = webRequest.GetRequestStream(); * // Write the data to the request stream. * dataStream.Write(byteArray, 0, byteArray.Length); * // Close the Stream object. * dataStream.Close(); * return webRequest; * }*/ public static ResponseT webSend <RequestT, ResponseT>(RequestT requestObj, CommunicationInformation comInfo, string originalGUID = null) { if (requestObj == null) { throw new ArgumentNullException("reqObj is null."); } if (comInfo == null) { throw new ArgumentNullException("commInfo is null."); } WebRequest request = WebRequest.Create(comInfo.URI); string strRequest = TransferTypeUtil.ObjToStr(requestObj); ResponseT responseObj; if (NeedPrintMessageToConsole) { Console.WriteLine("========== Print Request =========="); Console.WriteLine(strRequest); } byte[] byteArray = Encoding.UTF8.GetBytes(strRequest); request.Method = "POST"; request.ContentType = getContextType(comInfo.ContentType, comInfo.MessageProtocol); request.ContentLength = byteArray.Length; //If originalGUID is not null, use it //Otherwise if need spoofer, get OriginalGUID by TestCaseID; otherwise, generate a new GUID if (Convert.ToBoolean(CarConfigurationManager.AppSetting("NeedSpoofer")) == true) { uint nTestCaseID = CarCommonRequestGenerator.getTUIDFromRequest(requestObj); /// add by v-pxie 2013-11-22: support for the GUID list with one TUID string requestTypeName = requestObj.GetType().Name; originalGUID = EmbededFileOperation.getGUIDFromEmbededFile(nTestCaseID); } else if (originalGUID == null || originalGUID.Length == 0) { originalGUID = Guid.NewGuid().ToString(); } //add OriginalGUID in request header request.Headers.Add("e3jms-l-activityId-propname", "activityId"); request.Headers.Add("E3JMS-L-activityId", originalGUID); // Get the request stream. Stream dataStream = request.GetRequestStream(); // Write the data to the request stream. dataStream.Write(byteArray, 0, byteArray.Length); // Close the Stream object. dataStream.Close(); // Get the response. WebResponse response = request.GetResponse(); // Display the status. Console.WriteLine("Response.StatusDescription=" + ((HttpWebResponse)response).StatusDescription); // Get the stream containing content returned by the server. try { dataStream = response.GetResponseStream(); // Open the stream using a StreamReader for easy access. StreamReader reader = new StreamReader(dataStream); // Read the content. string responseFromServer = reader.ReadToEnd(); // Display the content. if (NeedPrintMessageToConsole) { Console.WriteLine("========== Print Response =========="); if (null != responseFromServer) { Console.WriteLine(responseFromServer); } } // Clean up the streams. reader.Close(); dataStream.Close(); response.Close(); responseObj = (ResponseT)TransferTypeUtil.strToObj(responseFromServer, typeof(ResponseT)); return(responseObj); } catch (Exception ex) { throw new Exception("Transport failed " + ex.ToString()); } }