コード例 #1
0
ファイル: ConsumirWS.cs プロジェクト: fabiodevft/NFse_Custom
        /// <summary>
        /// Estabelece conexão com o Webservice e faz o envio do XML e recupera o retorno. Conteúdo retornado pelo webservice pode ser recuperado através das propriedades RetornoServicoXML ou RetornoServicoString.
        /// </summary>
        /// <param name="xml">XML a ser enviado para o webservice</param>
        /// <param name="servico">Parâmetros para execução do serviço (parâmetros do soap)</param>
        /// <param name="certificado">Certificado digital a ser utilizado na conexão com os serviços</param>
        public void ExecutarServico(XmlDocument xml, object servico, X509Certificate2 certificado)
        {
            WSSoap soap = (WSSoap)servico;

            try
            {
                Uri    urlpost = new Uri(soap.EnderecoWeb);
                string soapXML = EnveloparXML(soap, "", xml.OuterXml);
                byte[] buffer2 = Encoding.UTF8.GetBytes(soapXML);

                ServicePointManager.Expect100Continue = false;
                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(RetornoValidacao);
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

                HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(urlpost);
                httpWebRequest.Headers.Add("SOAPAction: " + soap.ActionWeb);
                httpWebRequest.CookieContainer = cookies;
                httpWebRequest.Timeout         = 60000;
                httpWebRequest.ContentType     = (string.IsNullOrEmpty(soap.ContentType) ? "application/soap+xml; charset=utf-8;" : soap.ContentType);
                httpWebRequest.Method          = "POST";
                httpWebRequest.ClientCertificates.Add(certificado);
                httpWebRequest.ContentLength = buffer2.Length;

                Stream postData = httpWebRequest.GetRequestStream();
                postData.Write(buffer2, 0, buffer2.Length);
                postData.Close();

                HttpWebResponse responsePost         = (HttpWebResponse)httpWebRequest.GetResponse();
                Stream          streamPost           = responsePost.GetResponseStream();
                StreamReader    streamReaderResponse = new StreamReader(streamPost, Encoding.UTF8);

                XmlDocument retornoXml = new XmlDocument();
                retornoXml.LoadXml(streamReaderResponse.ReadToEnd());

                RetornoServicoString = retornoXml.GetElementsByTagName(soap.TagRetorno)[0].ChildNodes[0].OuterXml;
                RetornoServicoXML    = new XmlDocument
                {
                    PreserveWhitespace = false
                };
                RetornoServicoXML.LoadXml(RetornoServicoString);
            }
            catch
            {
                throw;
            }
        }
コード例 #2
0
ファイル: ConsumirWS.cs プロジェクト: fabiodevft/NFse_Custom
        /// <summary>
        /// Criar o envelope (SOAP) para envio ao webservice
        /// </summary>
        /// <param name="soap">Soap</param>
        /// <param name="xmlHeader">string do XML a ser enviado no cabeçalho do soap</param>
        /// <param name="xmlBody">string do XML a ser enviado no corpo do soap</param>
        /// <returns>string do envelope (soap)</returns>
        private static string EnveloparXML(WSSoap soap, string xmlHeader, string xmlBody)
        {
            string retorna = string.Empty;

            if (!string.IsNullOrEmpty(xmlHeader))
            {
                if (xmlHeader.IndexOf("?>") >= 0)
                {
                    xmlHeader = xmlHeader.Substring(xmlHeader.IndexOf("?>") + 2);
                }
            }

            if (xmlBody.IndexOf("?>") >= 0)
            {
                xmlBody = xmlBody.Substring(xmlBody.IndexOf("?>") + 2);
            }

            retorna = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";

            if (!string.IsNullOrEmpty(soap.SoapString))
            {
                //Soap string definido nas configurações
                retorna += soap.SoapString.Replace("{xmlHeader}", xmlHeader).Replace("{ActionWeb}", soap.ActionWeb).Replace("{xmlBody}", xmlBody);
            }
            else
            {
                //Soap string default
                retorna += "<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">";
                retorna += "<soap:Header>";
                retorna += xmlHeader;
                retorna += "</soap:Header>";
                retorna += "<soap:Body>";
                retorna += "<nfeDadosMsg xmlns=\"" + soap.ActionWeb + "\">";
                retorna += xmlBody;
                retorna += "</nfeDadosMsg>";
                retorna += "</soap:Body>";
                retorna += "</soap:Envelope>";
            }

            return(retorna);
        }