Exemple #1
0
        public virtual string HttpPost(string xmlRequest, Dictionary <string, string> config)
        {
            string logFile = null;

            if (IsValidConfigValueSet(config, "logFile"))
            {
                logFile = config["logFile"];
            }

            RequestTarget reqTarget = CommManager.instance(config).findUrl();
            var           uri       = reqTarget.getUrl();

            ServicePointManager.SecurityProtocol = ServicePointManager.SecurityProtocol | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11;
            var req = (HttpWebRequest)WebRequest.Create(uri);

            var neuterAccNums = false;

            if (config.ContainsKey("neuterAccountNums"))
            {
                neuterAccNums = ("true".Equals(config["neuterAccountNums"]));
            }

            var neuterCreds = false;

            if (config.ContainsKey("neuterUserCredentials"))
            {
                neuterCreds = ("true".Equals(config["neuterUserCredentials"]));
            }

            var printxml = false;

            if (config.ContainsKey("printxml"))
            {
                if ("true".Equals(config["printxml"]))
                {
                    printxml = true;
                }
            }

            if (printxml)
            {
                Console.WriteLine(xmlRequest);
                Console.WriteLine(logFile);
            }

            //log request
            if (logFile != null)
            {
                Log(xmlRequest, logFile, neuterAccNums, neuterCreds);
            }

            req.ContentType = "text/xml";
            req.Method      = "POST";
            req.ServicePoint.MaxIdleTime       = 8000;
            req.ServicePoint.Expect100Continue = false;
            req.KeepAlive = true;
            //req.Timeout = 500000;
            if (IsProxyOn(config))
            {
                var myproxy = new WebProxy(config["proxyHost"], int.Parse(config["proxyPort"]))
                {
                    BypassProxyOnLocal = true
                };
                req.Proxy = myproxy;
            }

            OnHttpAction(RequestType.Request, xmlRequest, neuterAccNums, neuterCreds);

            // submit http request
            using (var writer = new StreamWriter(req.GetRequestStream()))
            {
                writer.Write(xmlRequest);
            }


            string xmlResponse = null;

            // read response
            try
            {
                var resp = req.GetResponse();
                if (resp == null)
                {
                    return(null);
                }
                HttpWebResponse httpResp = (HttpWebResponse)resp;

                CommManager.instance().reportResult(reqTarget, CommManager.REQUEST_RESULT_RESPONSE_RECEIVED, (int)httpResp.StatusCode);

                using (var reader = new StreamReader(resp.GetResponseStream()))
                {
                    xmlResponse = reader.ReadToEnd().Trim();
                }
                if (printxml)
                {
                    Console.WriteLine(xmlResponse);
                }

                OnHttpAction(RequestType.Response, xmlResponse, neuterAccNums, neuterCreds);

                //log response
                if (logFile != null)
                {
                    Log(xmlResponse, logFile, neuterAccNums, neuterCreds);
                }
            } catch (WebException we)
            {
                int result = CommManager.REQUEST_RESULT_CONNECTION_FAILED;
                if (we.Status == WebExceptionStatus.Timeout)
                {
                    result = CommManager.REQUEST_RESULT_RESPONSE_TIMEOUT;
                }

                CommManager.instance().reportResult(reqTarget, result, 0);
            }
            return(xmlResponse);
        }
        private async Task <string> HttpPostCoreAsync(string xmlRequest, Dictionary <string, string> config, CancellationToken cancellationToken)
        {
            string logFile = null;

            if (IsValidConfigValueSet(config, "logFile"))
            {
                logFile = config["logFile"];
            }

            RequestTarget reqTarget = CommManager.instance().findUrl();
            var           uri       = reqTarget.getUrl();

            ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;
            var request = (HttpWebRequest)WebRequest.Create(uri);

            var neuter = false;

            if (config.ContainsKey("neuterAccountNums"))
            {
                neuter = ("true".Equals(config["neuterAccountNums"]));
            }

            var printxml = false;

            if (config.ContainsKey("printxml"))
            {
                if ("true".Equals(config["printxml"]))
                {
                    printxml = true;
                }
            }

            if (printxml)
            {
                Console.WriteLine(xmlRequest);
                Console.WriteLine(logFile);
            }

            //log request
            if (logFile != null)
            {
                Log(xmlRequest, logFile, neuter);
            }

            request.ContentType = ContentTypeTextXmlUTF8;
            request.Method      = "POST";
            request.ServicePoint.MaxIdleTime       = 8000;
            request.ServicePoint.Expect100Continue = false;
            request.KeepAlive = false;
            if (IsProxyOn(config))
            {
                var myproxy = new WebProxy(config["proxyHost"], int.Parse(config["proxyPort"]))
                {
                    BypassProxyOnLocal = true
                };
                request.Proxy = myproxy;
            }

            OnHttpAction(RequestType.Request, xmlRequest, neuter);

            // submit http request
            using (var writer = new StreamWriter(await request.GetRequestStreamAsync().ConfigureAwait(false)))
            {
                writer.Write(xmlRequest);
            }

            // read response
            string xmlResponse = null;
            var    response    = await request.GetResponseAsync().ConfigureAwait(false);

            HttpWebResponse httpResp = (HttpWebResponse)response;

            CommManager.instance().reportResult(reqTarget, CommManager.REQUEST_RESULT_RESPONSE_RECEIVED, Int32.Parse(httpResp.StatusCode.ToString()));
            try
            {
                using (var reader = new StreamReader(response.GetResponseStream()))
                {
                    xmlResponse = (await reader.ReadToEndAsync().ConfigureAwait(false)).Trim();
                }
                if (printxml)
                {
                    Console.WriteLine(xmlResponse);
                }

                OnHttpAction(RequestType.Response, xmlResponse, neuter);

                //log response
                if (logFile != null)
                {
                    Log(xmlResponse, logFile, neuter);
                }
            }catch (WebException we)
            {
                int result = CommManager.REQUEST_RESULT_CONNECTION_FAILED;
                if (we.Status == WebExceptionStatus.Timeout)
                {
                    result = CommManager.REQUEST_RESULT_RESPONSE_TIMEOUT;
                }
                CommManager.instance().reportResult(reqTarget, result, 0);
            }

            return(xmlResponse);
        }