[Eppnicbr] Epp em C#

Luiz Fernando Duarte Junior luizfduartejr at gmail.com
Mon Nov 24 11:50:12 BRST 2008


Basicamente os métodos abaixo são os que fazem todo o "trabalho braçal" do
Epp, enviando comandos e recebendo respostas.
Eles estão dentro da classe Acao, que é a superclasse de todos os comandos
Epp. Basicamente utilizei a biblioteca que o Registro.Br disponibiliza em
C++ para criar a minha arquitetura e até alguns métodos dela.

Qualquer dúvida, pode entrar em contato por este e-mail.

         /// <summary>
        /// Método utilizado para retornar um template xml de acordo com o
nome do template passado por parâmetro
        /// </summary>
        /// <param name="filename">O nome do arquivo Xml a ser
carregado</param>
        protected void LoadXmlTemplate(string filename)
        {
            string sLine = "";
            this._xml = "";

            if (filename != "")
            {
                StreamReader objReader = new
StreamReader(CAMINHO_TEMPLATES_XML + filename);

                while (sLine != null)
                {
                    sLine = objReader.ReadLine();
                    if (sLine != null && sLine != "")
                    {
                        this._xml += " " + sLine;
                    }
                }
                objReader.Close();
            }
        }

        /// <summary>
        /// Método responsável por enviar o comando para o registro.br e
receber a resposta do mesmo
        /// </summary>
        /// <returns>Retorna uma string contendo a resposta do servidor em
Xml</returns>
        protected string Executar()
        {
            try
            {
                // enviar XML
                byte[] bytesXml = Encoding.UTF8.GetBytes(this._xml.Trim());
                this.EscreverBuffer(bytesXml.Length + 4);
                GerenciadorConexao.StreamSSL.Write(bytesXml, 0,
bytesXml.Length);
                GerenciadorConexao.StreamSSL.Flush();

                // receber XML
                int len = LerBuffer();
                byte[] bytes = this.LerMensagem(len - 4);

                return Encoding.UTF8.GetString(bytes, 0, bytes.Length);
            }
            catch (System.Exception ex)
            {
                GerenciadorConexao.StreamSSL = null;
                throw ex;
            }
        }

        /// <summary>
        /// Método responsável por escrever o buffer da SSLStream
        /// </summary>
        /// <param name="buf_sz">O tamanho do buffer a ser escrito</param>
        private void EscreverBuffer(int buf_sz)
        {
            byte[] out_buf = new byte[4];
            out_buf[0] = (byte)(0xff & (buf_sz >> 24));
            out_buf[1] = (byte)(0xff & (buf_sz >> 16));
            out_buf[2] = (byte)(0xff & (buf_sz >> 8));
            out_buf[3] = (byte)(0xff & buf_sz);

            GerenciadorConexao.StreamSSL.Write(out_buf, 0, 4);
        }

        /// <summary>
        /// Método responsável por ler o buffer da SSLStream
        /// </summary>
        /// <returns>Retornar um inteiro representando o tamanho do buffer
lido</returns>
        private int LerBuffer()
        {
            byte[] in_buf = new byte[4];

            int len = 0;
            int bytesRead = 0;
            while (bytesRead < 4)
            {
                try
                {
                    len = GerenciadorConexao.StreamSSL.Read(in_buf,
bytesRead, 4 - bytesRead);
                }
                catch (System.Exception ex)
                {//IOException
                    //throw ex;
                    GerenciadorConexao.StreamSSL = null;
                    return -1;
                }
                if (len < 0)
                {
                    return -1;
                }
                bytesRead += len;
            }

            return (((in_buf[0] & 0xff) << 24) | ((in_buf[1] & 0xff) << 16)
|
                    ((in_buf[2] & 0xff) << 8) | (in_buf[3] & 0xff));
        }

        /// <summary>
        /// Método responsável por ler a mensagem passada em buffer
        /// </summary>
        /// <param name="inbuf_sz">O tamanho do buffer a ser lido</param>
        /// <returns>Retorna um array de bytes representando a mensagem
lida</returns>
        private byte[] LerMensagem(int inbuf_sz)
        {
            byte[] in_buf = new byte[inbuf_sz];

            int len = 0;
            int bytesRead = 0;
            while (bytesRead < inbuf_sz)
            {
                try
                {
                    len = GerenciadorConexao.StreamSSL.Read(in_buf,
bytesRead, inbuf_sz - bytesRead);
                }
                catch (System.Exception xcp)
                {//IOException
                    GerenciadorConexao.StreamSSL = null;
                    return null;
                    //throw xcp;
                }
                if (len < 0)
                {
                    return null;
                }
                bytesRead += len;
            }
            return in_buf;
        }

2008/11/24 <giuseppe at corp.plugin.com.br>

> Tem algum exemplo que utilize o beta do registro que possa disponibilizar?
>
> Desde ja obrigado.
>
> [ ]'s
> Giuseppe Lopes
>
> > Opa, valeu mas já terminei faz mais de 3 meses, rsrsrsrsrs
> >
> > Na verdade, já estou bolando uma nova versão do sistema, estou pensando
> em
> > utilizar Linq To Xml e parar de manipular os Xmls de envio e resposta
> como
> > se fossem strings.
> >
> > Mas obrigado por perguntar.
> >
> > Atenciosamente,
> >
> > Luiz Fernando
> >
> > 2008/11/23 Anderson L K <alk at sisgel.com>
> >
> >>  Como está a implementação em C#?
> >>
> >> Precisa de ajuda?
> >>
> >>
> >> Anderson L K
> >>
> >> _______________________________________________
> >> eppnicbr mailing list
> >> eppnicbr at eng.registro.br
> >> https://eng.registro.br/mailman/listinfo/eppnicbr
> >>
> >>
> > _______________________________________________
> > eppnicbr mailing list
> > eppnicbr at eng.registro.br
> > https://eng.registro.br/mailman/listinfo/eppnicbr
> >
>
>
> _______________________________________________
> eppnicbr mailing list
> eppnicbr at eng.registro.br
> https://eng.registro.br/mailman/listinfo/eppnicbr
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://eng.registro.br/pipermail/eppnicbr/attachments/20081124/62ea2f61/attachment.html>


More information about the eppnicbr mailing list