Code Monkey home page Code Monkey logo

Comments (4)

rftd avatar rftd commented on August 10, 2024

Enviado uma possível correção, nos meus teste deu erro 503 e não 400.

from openac.net.nfse.

Transis-Felipe avatar Transis-Felipe commented on August 10, 2024

Show, vou testar e te aviso. Obrigado

from openac.net.nfse.

Transis-Felipe avatar Transis-Felipe commented on August 10, 2024

Usando a rotina genérica (Não retorna nada):

image

Usando a rotina de envio override (Se chegou a retornar essas infos, deu certo a comunicação):

image
Se usar esse código no IPM2 a comunicação funciona, como no print acima.

    public string ConsultarNFSe(string cabec, string msg) => UploadHttpClient(msg);

    protected string UploadHttpClient(string message)
    {
        var url = Url;

        try
        {
            var auth = Authentication();
            var fileName = $"{DateTime.Now:yyyyMMddssfff}_{PrefixoEnvio}_envio.xml";
            var arquivoEnvio = Path.Combine(Path.GetTempPath(), fileName);
            File.WriteAllText(arquivoEnvio, message);

            var client = new HttpClient();
            var request = new HttpRequestMessage(HttpMethod.Post, Url);
            request.Headers.Add("Authorization", auth);

            var content = new StringContent(message);

            request.Content = content;
            var response = client.SendAsync(request).Result;
            response.EnsureSuccessStatusCode();

            EnvelopeRetorno = response.Content.ReadAsStringAsync().Result;

            return EnvelopeRetorno;
        }
        finally
        {
            Url = url;
        }
    }

Vou te passar no discord o login e senha para testar o caso do IPM2

from openac.net.nfse.

Transis-Felipe avatar Transis-Felipe commented on August 10, 2024

Abaixo o print do teste que não deu certo na rotina genérica:

image

Aqui com a rotina override que funciona no IPM:

image

    public string ConsultarLoteRps(string cabec, string msg) => UploadIPM(msg);

    protected string UploadIPM(string message)
    {
        var url = Url;

        try
        {
            var auth = Authentication();
            var fileName = $"{DateTime.Now:yyyyMMddssfff}_{PrefixoEnvio}_envio.xml";
            var arquivoEnvio = Path.Combine(Path.GetTempPath(), fileName);
            File.WriteAllText(arquivoEnvio, message);

            //Identificate separator
            string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
            //Encoding
            byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");

            //Creation and specification of the request
            HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url); //sVal is id for the webService
            wr.ContentType = "multipart/form-data; boundary=" + boundary;
            wr.Method = "POST";
            wr.KeepAlive = true;
            wr.Credentials = System.Net.CredentialCache.DefaultCredentials;

            wr.Headers.Add("Authorization: " + auth); //AUTHENTIFICATION END
            Stream rs = wr.GetRequestStream();


            //string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}"; //For the POST's format

            //Writting of the file
            rs.Write(boundarybytes, 0, boundarybytes.Length);
            byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(arquivoEnvio);
            rs.Write(formitembytes, 0, formitembytes.Length);

            rs.Write(boundarybytes, 0, boundarybytes.Length);

            string headerTemplate = $"Content-Disposition: form-data; name=\"file\"; filename=\"{fileName}\"\r\nContent-Type: text/xml\r\n\r\n";
            byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(headerTemplate);
            rs.Write(headerbytes, 0, headerbytes.Length);

            FileStream fileStream = new FileStream(arquivoEnvio, FileMode.Open, FileAccess.Read);
            byte[] buffer = new byte[4096];
            int bytesRead = 0;
            while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
            {
                rs.Write(buffer, 0, bytesRead);
            }
            fileStream.Close();

            byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
            rs.Write(trailer, 0, trailer.Length);
            rs.Close();
            rs = null;

            WebResponse wresp = null;
            try
            {
                //Get the response
                wresp = wr.GetResponse();
                Stream stream2 = wresp.GetResponseStream();
                StreamReader reader2 = new StreamReader(stream2);
                EnvelopeRetorno = reader2.ReadToEnd();
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
            finally
            {
                if (wresp != null)
                {
                    wresp.Close();
                    wresp = null;
                }
                wr = null;
            }

            return EnvelopeRetorno;
        }
        finally
        {
            Url = url;
        }
    }

from openac.net.nfse.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.