Ejemplo n.º 1
0
        public static string EnviaSefaz(DFeSoapConfig soapConfig)
        {
            string XMLRetorno = string.Empty;
            string xmlSoap    = Envelope.Construir(soapConfig);

            Uri uri = new Uri(soapConfig.Url);

            WebRequest     webRequest = WebRequest.Create(uri);
            HttpWebRequest httpWR     = (HttpWebRequest)webRequest;

            httpWR.Timeout = soapConfig.TimeOut == 0 ? 2000 : soapConfig.TimeOut;

            httpWR.ContentLength = Encoding.UTF8.GetBytes(xmlSoap).Length;

            httpWR.ClientCertificates.Add(soapConfig.Certificado);

            httpWR.ComposeContentType("application/soap+xml", Encoding.UTF8, soapConfig.Metodo);

            httpWR.Method = "POST";

            Stream       reqStream    = httpWR.GetRequestStream();
            StreamWriter streamWriter = new StreamWriter(reqStream);

            streamWriter.Write(xmlSoap, 0, Encoding.UTF8.GetBytes(xmlSoap).Length);
            streamWriter.Close();

            WebResponse  webResponse  = httpWR.GetResponse();
            Stream       respStream   = webResponse.GetResponseStream();
            StreamReader streamReader = new StreamReader(respStream);

            XMLRetorno = streamReader.ReadToEnd();

            return(XMLRetorno);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Cria e envia a requisição HttpClient, retornando a resposta obtida do WebService.
        /// </summary>
        /// <param name="xmlEnvelop"></param>
        /// <param name="certificadoDigital"></param>
        /// <param name="url"></param>
        /// <param name="timeOut"></param>
        /// <param name="tipoEvento"></param>
        /// <returns></returns>
        public async Task <string> SendRequestAsync(XmlDocument xmlEnvelop, X509Certificate2 certificadoDigital, string url, int timeOut, TipoEvento?tipoEvento = null, string actionUrn = "")
        {
            //verifica se pelo menos uma das 2 propriedades obrigatorias estão definidas
            if (!tipoEvento.HasValue && string.IsNullOrWhiteSpace(actionUrn))
            {
                throw new ArgumentNullException("Pelo menos uma das propriedades tipoEvento ou actionUrl devem ser definidos para executar a action na requisição soap");
            }

            //caso o tipoevento esteja definido, pega a url do evento
            if (tipoEvento.HasValue)
            {
                actionUrn = new SoapUrls().GetSoapUrl(tipoEvento.Value);
            }

            string         xmlSoap = xmlEnvelop.InnerXml;
            HttpWebRequest httpWr  = (HttpWebRequest)WebRequest.Create(new Uri(url));

            httpWr.Timeout       = timeOut == 0 ? 2000 : timeOut;
            httpWr.ContentLength = Encoding.UTF8.GetBytes(xmlSoap).Length;
            httpWr.ClientCertificates.Add(certificadoDigital);
            httpWr.ComposeContentType("application/soap+xml", Encoding.UTF8, actionUrn);
            httpWr.Method = "POST";

            StreamWriter streamWriter = new StreamWriter(httpWr.GetRequestStream());

            streamWriter.Write(xmlSoap, 0, Encoding.UTF8.GetBytes(xmlSoap).Length);
            streamWriter.Close();

            using (HttpWebResponse httpResponse = (HttpWebResponse)httpWr.GetResponse())
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    string xmlRetorno = streamReader.ReadToEnd();
                    return(await Task.FromResult(xmlRetorno));
                }
        }