/// <summary>
        /// Executa o carregamento, execução do soap e retorno do resultado
        /// </summary>
        /// <typeparam name="TCommonSoapEnvelope"></typeparam>
        /// <param name="soapEnvelope"></param>
        /// <param name="configuration"></param>
        /// <param name="responseElementName"></param>
        /// <param name="actionUrn">Qual é a action (urn) a ser executada, NÃO é a url do server do sefaz! A url esta dentro da configuração(configuration)</param>
        /// <returns></returns>
        public static XmlNode Execute <TCommonSoapEnvelope>(TCommonSoapEnvelope soapEnvelope,
                                                            WsdlConfiguracao configuration, string actionUrn, string responseElementName) where TCommonSoapEnvelope : CommonSoapEnvelope
        {
            SoapUtils   soapUtils = new SoapUtils();
            XmlDocument xmlResult = new XmlDocument();

            var xmlEnvelop = soapUtils.SerealizeDocument(soapEnvelope);

            string tes = soapUtils.SendRequest(xmlEnvelop, configuration.CertificadoDigital, configuration.Url, configuration.TimeOut, actionUrn: actionUrn);

            xmlResult.LoadXml(tes);

            return(xmlResult.GetElementsByTagName(responseElementName)[0]);
        }
        /// <summary>
        /// Executa o carregamento, execução do soap e retorno do resultado
        /// </summary>
        /// <typeparam name="TCommonSoapEnvelope"></typeparam>
        /// <param name="soapEnvelope"></param>
        /// <param name="configuration"></param>
        /// <param name="requestType">Especifico para CTe</param>
        /// <param name="responseElementName"></param>
        /// <returns></returns>
        public static async Task <XmlNode> ExecuteAsync <TCommonSoapEnvelope>(TCommonSoapEnvelope soapEnvelope,
                                                                              WsdlConfiguracao configuration, TipoEvento requestType, string responseElementName) where TCommonSoapEnvelope : CommonSoapEnvelope
        {
            SoapUtils   soapUtils = new SoapUtils();
            XmlDocument xmlResult = new XmlDocument();

            var xmlEnvelop = soapUtils.SerealizeDocument(soapEnvelope);

            string tes = await soapUtils.SendRequestAsync(xmlEnvelop, configuration.CertificadoDigital, configuration.Url, configuration.TimeOut, requestType);

            xmlResult.LoadXml(tes);

            return(xmlResult.GetElementsByTagName(responseElementName)[0]);
        }
Beispiel #3
0
        /// <summary>
        /// Constructs a SOAP envelope request, with a body that includes the operation as element and the W3C Document and saves the SDMX Part of the response to the specified ouput
        /// The W3C Document contains either a SDMX-ML Query or a SDMX-ML Registry Interface
        /// </summary>
        /// <param name="request">
        /// The W3C Document representation of a SDMX-ML Query or QueryStructureRequest
        /// </param>
        /// <param name="webServiceOperation">
        /// The Web Service function
        /// </param>
        /// <param name="tempFileName">
        /// The temporary file name
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// request is null
        /// </exception>
        /// <exception cref="NsiClientException">
        /// Error in server response or communication
        /// </exception>
        private void SendRequest(
            XmlDocument request,
            SDMXWSFunction webServiceOperation,
            string tempFileName)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            NsiClientHelper.LogSdmx(request);
            var sb = new StringBuilder();

            sb.AppendFormat(SoapConstants.SoapRequest, this._config.Prefix, this._wsdlConfig.GetTargetNamespace());
            var doc = new XmlDocument();

            doc.LoadXml(sb.ToString());
            string      operationName = webServiceOperation.ToString();
            XmlNodeList nodes         = doc.GetElementsByTagName(SoapConstants.Body, SoapConstants.Soap11Ns);
            XmlElement  operation     = doc.CreateElement(
                this._config.Prefix, operationName, this._wsdlConfig.GetTargetNamespace());

            XmlElement queryParent   = operation;
            string     parameterName = this._wsdlConfig.GetParameterName(operationName);

            if (!string.IsNullOrEmpty(parameterName))
            {
                queryParent = doc.CreateElement(
                    this._config.Prefix, parameterName, this._wsdlConfig.GetTargetNamespace());
                operation.AppendChild(queryParent);
            }

            if (request.DocumentElement != null)
            {
                XmlNode sdmxQueryNode = doc.ImportNode(request.DocumentElement, true);
                queryParent.AppendChild(sdmxQueryNode);
            }

            nodes[0].AppendChild(operation);

            var endpointUri = new Uri(this._config.EndPoint);
            var webRequest  = (HttpWebRequest)WebRequest.Create(endpointUri);

            webRequest.Headers.Add("Accept-Encoding", "gzip, deflate");
            webRequest.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;

            string soapAction = this._wsdlConfig.GetSoapAction(operationName);

            //if (soapAction != null && soapAction != "")
            if (soapAction != null)
            {
                webRequest.Headers.Add(SoapConstants.SoapAction, soapAction);
            }

            webRequest.ContentType = HttpConstants.Content;

            // webRequest.Accept = "text/xml";
            webRequest.Method  = HttpConstants.Post;
            webRequest.Timeout = 1800 * 1000;
            this.SetupWebRequestAuth(webRequest);

            using (Stream stream = webRequest.GetRequestStream())
            {
                doc.Save(stream);
            }

            try
            {
                using (WebResponse response = webRequest.GetResponse())
                {
                    using (Stream stream = response.GetResponseStream())
                    {
                        if (stream != null)
                        {
                            var settings = new XmlWriterSettings();
                            settings.Indent = true;
                            using (XmlWriter writer = XmlWriter.Create(tempFileName, settings))
                            {
                                SoapUtils.ExtractSdmxMessage(stream, writer);
                            }
                            NsiClientHelper.LogSdmx(tempFileName, Resources.InfoSoapResponse);
                        }
                    }
                }
            }
            catch (WebException ex)
            {
                NsiClientHelper.HandleSoapFault(ex);
            }
        }