コード例 #1
0
        /// add by v-pxie 2013-11-22: support for the GUID list with one TUID and requestNmae
        public static HttpWebResponse SendReqAndRecvResp(MemoryStream ms, CommunicationInformation commInfo, string requestTypeName = null)
        {
            if (ms == null)
            {
                throw new ArgumentNullException("ms");
            }

            if (commInfo == null)
            {
                throw new ArgumentNullException("commInfo");
            }

            HttpWebRequest httpWebRequest = CreateRequestHeader(commInfo);

            // If need spoofer, get OriginalGUID by TestCaseID; otherwise, generate a new GUID
            string originalGUID = "";

            if (Convert.ToBoolean(CarConfigurationManager.AppSetting("NeedSpoofer")) == true)
            {
                /// add by v-pxie 2013-11-22: support for the GUID list with one TUID and requestNmae
                originalGUID = EmbededFileOperation.getGUIDFromEmbededFile(commInfo.TestCaseID);
            }
            else
            {
                originalGUID = Guid.NewGuid().ToString();
            }

            // E3JMS-L-activityId
            // Note: If need Spoofer, whatever the value in WithE3JRequestHeader, we all need to add OriginalGUID in request header.
            if (Convert.ToBoolean(CarConfigurationManager.AppSetting("NeedSpoofer")) == true || commInfo.WithE3JRequestHeader)
            {
                httpWebRequest.Headers.Add("e3jms-l-activityId-propname", "activityId");
                httpWebRequest.Headers.Add("E3JMS-L-activityId", originalGUID);
            }

            // Send E3J request with httpWebRequest.Headers.
            if (commInfo.WithE3JRequestHeader)
            {
                // E3JMS-L-origServerName
                httpWebRequest.Headers.Add("E3JMS-L-origServerName", Environment.MachineName);

                // E3JMS-L-serviceName
                httpWebRequest.Headers.Add("E3JMS-L-serviceName", System.Diagnostics.Process.GetCurrentProcess().ProcessName);

                // e3jms-l-stand-rep
                httpWebRequest.Headers.Add("e3jms-l-stand-rep", "TQ:e3Interop");

                // E3JMS-L-stand-mes
                String messageId = originalGUID + "-" + Guid.NewGuid();
                httpWebRequest.Headers.Add("E3JMS-L-stand-mes", messageId);
            }

            if (commInfo.HttpSendMode == CommunicationInformation.HTTPSendMode.POST)
            {
                httpWebRequest.Accept = httpWebRequest.ContentType;
            }

            httpWebRequest.ContentLength = ms.GetBuffer().Length;
            if (httpWebRequest.ContentLength > 0)
            {
                httpWebRequest.GetRequestStream().Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
            }
            return(SendRequestAndReceiveResponse(httpWebRequest));
        }
コード例 #2
0
        /*
         * 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());
            }
        }