//Send Http request to tools, E.g: CPCAT, Car internal admin tool public static string SendToolHttpRequestAndReceiveResponse(String url) { try { //Print the Http request to tool,E.g: CPCAT //Create a new HttpWebRequest HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Pipelined = false; request.Method = "POST"; //Set headers request.Headers.Add("x-prototype-version", "1.6.0.3"); request.Headers.Add("x-requested-with", "XMLHttpRequest"); request.Headers.Add("UA-CPU", "x86"); request.Headers.Add("Pragma", "no-cache"); request.ContentType = "application/xml"; request.Timeout = 150000; // Set credentials to use for this request. request.Credentials = CredentialCache.DefaultCredentials; //Send request to tool(E.g CPCAT) and get response HttpWebResponse response = (HttpWebResponse)request.GetResponse(); //Convert response to string for investigation if needed. byte[] responseBytes = TransferTypeUtil.ReadStream(response.GetResponseStream()); string responseString = TransferTypeUtil.ByteArrayToString(responseBytes, CarCommonEnumManager.EncodingType.ASCII); return(responseString); } catch (Exception e) { Assert.Fail("Http request sending fail, exception is: " + e.ToString()); return(""); } }
public static string send(string strRequest, CommunicationInformation commInfo) { if (commInfo.HttpSendMode == CommunicationInformation.HTTPSendMode.POST && string.IsNullOrEmpty(strRequest)) { Assert.Fail("Can't send request, request object is null."); } if (commInfo == null) { Assert.Fail("Can't send request, CommunicationInfo object is null."); } WebRequest request = CreateWebRequestHeader(commInfo); request.Timeout = System.Threading.Timeout.Infinite; Stream dataStream; request.Timeout = System.Threading.Timeout.Infinite; if (commInfo.HttpSendMode == CommunicationInformation.HTTPSendMode.POST) { // Print request Console.WriteLine("Request content:\r\n" + strRequest); byte[] byteArray = Encoding.UTF8.GetBytes(strRequest); request.ContentLength = byteArray.Length; request.GetRequestStream().Write(byteArray, 0, byteArray.Length); } try { // 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. 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. Console.WriteLine("Response content:\r\n" + TransferTypeUtil.ObjToStr(responseFromServer)); // Clean up the streams. reader.Close(); dataStream.Close(); response.Close(); return(responseFromServer); } catch (WebException ex) { HttpWebResponse x = ex.Response as HttpWebResponse; dataStream = x.GetResponseStream(); // Open the stream using a StreamReader for easy access. StreamReader reader1 = new StreamReader(dataStream); // Read the content. return(x.StatusCode + ":" + reader1.ReadToEnd()); } }
/* * 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()); } }
public static MemoryStream DeSerialize(MemoryStream ms) { if (ms == null || ms.Length == 0) { throw new ArgumentNullException("ms"); } ms.Seek(0, SeekOrigin.Begin); //Log the response before deserializing for debugging. bool needPrintMsg = false; if (null != CarConfigurationManager.AppSetting("NeedPrintResponseBeforeDeserialize")) { needPrintMsg = Boolean.Parse(CarConfigurationManager.AppSetting("NeedPrintResponseBeforeDeserialize")); } if (needPrintMsg) { Console.WriteLine(TransferTypeUtil.ObjToStr(ms)); } XmlDocument doc = new XmlDocument(); using (XmlReader reader = XmlFastInfosetReader.Create(ms)) { doc.Load(reader); } //TODO: below handling is just to unblock testing, need to delete later to defect the DateTime handling bug //Delete the time in MinDate and MaxDate to unblock testing XmlNodeList minDateNodeList = doc.GetElementsByTagName("ns2:MinDate"); XmlNodeList maxDateNodeList = doc.GetElementsByTagName("ns2:MaxDate"); if (null != minDateNodeList && minDateNodeList.Count > 0) { foreach (XmlNode minDateNode in minDateNodeList) { minDateNode.InnerText = minDateNode.InnerText.Substring(0, 10); } } if (null != maxDateNodeList && maxDateNodeList.Count > 0) { foreach (XmlNode maxDateNode in maxDateNodeList) { maxDateNode.InnerText = maxDateNode.InnerText.Substring(0, 10); } } //Update 24:00 to 23:59 to unblock testing XmlNodeList minTimeNodeList = doc.GetElementsByTagName("ns2:MinTime"); XmlNodeList maxTimeNodeList = doc.GetElementsByTagName("ns2:MaxTime"); if (null != maxTimeNodeList && maxTimeNodeList.Count > 0) { foreach (XmlNode maxTimeNode in maxTimeNodeList) { string maxTime = maxTimeNode.InnerText; maxTime = maxTime.Replace("24:00", "23:59"); maxTimeNode.InnerText = maxTime; } } if (null != minTimeNodeList && minTimeNodeList.Count > 0) { foreach (XmlNode minTimeNode in minTimeNodeList) { string minTime = minTimeNode.InnerText; minTime = minTime.Replace("24:00", "23:59"); minTimeNode.InnerText = minTime; } } MemoryStream msDeserialized = new MemoryStream(); using (XmlWriter writer = XmlWriter.Create(msDeserialized)) { doc.WriteContentTo(writer); writer.Flush(); writer.Close(); } msDeserialized.Seek(0L, SeekOrigin.Begin); return(msDeserialized); }