GNRE Webservice Integration with C#

Asked

Viewed 1,590 times

0

Someone can help me or has been through this trouble?

I am doing a project to integrate my software with the GNRE webservice to send the guides in an automated way.

In the integration with Webservice I am sending the SOAP package below, but I am with the following answer:

The remote server returned an error: (500) Internal Server Error

I was looking at some posts and I was told that it may also be blocking Firewal.

Detail, I already have the digital certificate of the company installed and configured.

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
< soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
< soap:Header>
< gnreCabecMsg xmlns="http://www.gnre.pe.gov.br/webservice/GnreLoteRecepcao">
< versaoDados>1.00</versaoDados>
< /gnreCabecMsg>
< /soap:Header>
< soap:Body>
< gnreDadosMsg xmlns="http://www.gnre.pe.gov.br/webservice/GnreLoteRecepcao">
< TLote_GNRE xmlns="http://www.gnre.pe.gov.br">
< guias>
< TDadosGNRE>
< c01_UfFavorecida>PE</c01_UfFavorecida>
< c02_receita>100099</c02_receita>
< c26_produto>20</c26_produto>
< c27_tipoIdentificacaoEmitente>1</c27_tipoIdentificacaoEmitente>
< c03_idContribuinteEmitente><CNPJ>XXXXXX</CNPJ></c03_idContribuinteEmitente>
< c06_valorPrincipal>0.01</c06_valorPrincipal>
< c14_dataVencimento>2015-06-05</c14_dataVencimento>
< c15_convenio>49-Protocolo</c15_convenio>
< c16_razaoSocialEmitente>XXXXXX</c16_razaoSocialEmitente>
< c18_enderecoEmitente>XXXXXX</c18_enderecoEmitente>
< c19_municipioEmitente>50308</c19_municipioEmitente>
< c20_ufEnderecoEmitente>SP</c20_ufEnderecoEmitente>
< c21_cepEmitente>XXXXXX</c21_cepEmitente>
< c22_telefoneEmitente>XXXXXX</c22_telefoneEmitente>
< c34_tipoIdentificacaoDestinatario>1</c34_tipoIdentificacaoDestinatario>
< c35_idContribuinteDestinatario><CNPJ>XXXXXXXXXX</CNPJ></c35_idContribuinteDestinatario>
< c37_razaoSocialDestinatario>XXXXXXXXXXXXXXXXX</c37_razaoSocialDestinatario>
< c38_municipioDestinatario>00054</c38_municipioDestinatario>
< c33_dataPagamento>2015-06-05</c33_dataPagamento>
< c05_referencia><mes>06</mes><ano>2015</ano></c05_referencia>
< c39_camposExtras>
< campoExtra>
< codigo>9</codigo><tipo>T</tipo><valor>XXXXXXXXXXXXXXXXXXXXXXXX</valor>
< /campoExtra>
< /c39_camposExtras>
< /TDadosGNRE></guias></TLote_GNRE></gnreDadosMsg>
< /soap:Body>
< /soap:Envelope>

I am using the following C# code for web service integration:

public HttpWebRequest RequisicaoGNRELoteRecepcao()
{           
  HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create  (@"https://www.testegnre.pe.gov.br/gnreWS/services/GnreLoteRecepcao");
  string file;
  file = @"C:\erp\Certificado.cer";
  X509Certificate cer = X509Certificate.CreateFromCertFile(file);
  webRequest.ClientCertificates.Add(cer);
  webRequest.Headers.Add(@"SOAP:Action");
  webRequest.ContentType = "text/xml;charset=\"utf-8\"";
  webRequest.Accept = "text/xml";
  webRequest.Method = "POST";
  return webRequest;
}

public string WebServiceGNREEnviarLote(string arquivo, string xml)
{
  HttpWebRequest request = RequisicaoGNRELoteRecepcao();
  XmlDocument soapEnvelopeXml = new XmlDocument();          
  System.Net.ServicePointManager.CertificatePolicy = new MyPolicy();
  soapEnvelopeXml.LoadXml(xml);
  using (Stream stream = request.GetRequestStream())
  {
    soapEnvelopeXml.Save(stream);
  }
  using (WebResponse response = request.GetResponse())
  {
    using (StreamReader rd = new StreamReader(response.GetResponseStream()))
    {
      string soapResult = rd.ReadToEnd();
      return soapResult;
    }
  }                 
}
  • Error 500 is internal server error. It is some error of their code there.

  • Your tags have space (e.g.: < TDadosGNRE>)? The right thing wouldn’t be without space <TDadosGNRE>? Post the c# code you use to send the Soap.

2 answers

1

The mistake 500 Internal Server Error occurs when:

  • Failure in server configuration
  • A server dependency has stopped working
  • Instructions sent incorrectly to the headers or to the content body

Note that your SOAP has spaces at the beginning of several tags and this may be the cause, as I said earlier (Instructions sent incorrectly to the headers or to the content body), as an example:

< soap:Envelope, < soap:Header>, < gnreCabecMsg, < versaoDados>, < /gnreCabecMsg>, < /soap:Header>, < soap:Body>, < TLote_GNRE, < guias>, etc..

I recommend you fix this.

One way to send SOAP with c# (csharp) is to use something like this reply on Soen:

using System.Xml;
using System.Net;
using System.IO;

public static void CallWebService()
{
    var _url = "http://xxxxxxxxx/Service1.asmx";
    var _action = "http://xxxxxxxx/Service1.asmx?op=HelloWorld";

    XmlDocument soapEnvelopeXml = CreateSoapEnvelope();
    HttpWebRequest webRequest = CreateWebRequest(_url, _action);
    InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);

    // begin async call to web request.
    IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);

    // suspend this thread until call is complete. You might want to
    // do something usefull here like update your UI.
    asyncResult.AsyncWaitHandle.WaitOne();

    // get the response from the completed web request.
    string soapResult;
    using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
    {
        using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
        {
            soapResult = rd.ReadToEnd();
        }
        Console.Write(soapResult);        
    }
}

private static HttpWebRequest CreateWebRequest(string url, string action)
{
    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
    webRequest.Headers.Add("SOAPAction", action);
    webRequest.ContentType = "text/xml;charset=\"utf-8\"";
    webRequest.Accept = "text/xml";
    webRequest.Method = "POST";
    return webRequest;
}

private static XmlDocument CreateSoapEnvelope()
{
    XmlDocument soapEnvelop = new XmlDocument();
    soapEnvelop.LoadXml(@"CONTEUDO DO SEU SOAP AQUI");
    return soapEnvelop;
}

private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
{
    using (Stream stream = webRequest.GetRequestStream())
    {
        soapEnvelopeXml.Save(stream);
    }
}
  • As requested see the code . NET I am using to send the request to the webservice and returning the error already reported:

  • @Good morning, where did you add the code? I could not find, tried to tidy up the XML as mentioned?

  • The XML for some reason got blank space only when I posted, but in my code is without space.

  • @Alexandre you said tried to add the code . net in the comment? Try to edit the question and add there, in the comments this does not appear, grateful :)

  • public string Webservicegnreenviarlote(string file, string xml) { Httpwebrequest request = Requisicaognrelotereception(); Xmldocument soapEnvelopeXml = new Xmldocument(); System.Net.Servicepointmanager.Certificatepolicy = new Mypolicy(); soapEnvelopeXml.Loadxml(xml); using (Stream stream = request.Getrequeststream()) { soapEnvelopeXml.Save(stream); } using (Webresponse Response = request.Getresponse()) {using (Streamreader Rd = new Streamreader(Response.Getresponsestream()) { string soapResult = Rd.Readtoend(); Return soapResult; } } }

  • the error (500) ok on that snippet of the above code: using (Webresponse Response = request.Getresponse())

  • @Alexandre tried to use the code I posted?

  • I’m not using that code:

  • public httpwebrequest requisicaognreloterecepcao() { &#xA;HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(@"https://www.testegnre.pe.gov.br/gnreWS/services/GnreLoteRecepcao");&#xA;string file;&#xA;file = @"C:\erp\Certificado.cer";&#xA;X509Certificate cer = X509Certificate.CreateFromCertFile(file);&#xA;webRequest.ClientCertificates.Add(cer);&#xA;webRequest.Headers.Add(@"SOAP:Action");&#xA;webRequest.ContentType = "text/xml;charset="utf-8"";&#xA;webRequest.Accept = "text/xml";&#xA;webRequest.Method = "POST";&#xA;return webRequest;&#xA;}

  • and to send using the first posted: Webservicegnreenviarlote

  • I’ve already added the code I’m using to the question

  • Thanks for posting the whole code @Alexandre but you got to test what I posted?

  • Apparently it looks like my code.. Do you think that using the code you posted might solve error 500? At what point do you think you’re wrong? Thank you.

  • It is interesting that when I try to access the website address, the message from the web browser appears:Your connection is not particular. Attackers may be trying to steal your information from www.testegnre.pe.gov.br NET:ERR_CERT_AUTHORITY_INVALID. Might this also not be affecting my request on the webservice?

  • Guy managed to connect on web service ?

Show 10 more comments

-1

The error is in xml; use the following:

const string xml = @"<?xml version=""1.0"" encoding=""utf-8"" standalone=""yes""?>
                            <soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" 
                                           xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" 
                                           xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">
                            <soap:Header>
                            <gnreCabecMsg xmlns=""http://www.gnre.pe.gov.br/webservice/GnreLoteRecepcao"">
                            <versaoDados>1.00</versaoDados>
                            </gnreCabecMsg>
                            </soap:Header>
                            <soap:Body>
                            <gnreDadosMsg xmlns=""http://www.gnre.pe.gov.br/webservice/GnreLoteRecepcao"">
                            <TLote_GNRE xmlns=""http://www.gnre.pe.gov.br"">
                            <guias>
                            <TDadosGNRE>
                            <c01_UfFavorecida>PE</c01_UfFavorecida>
                            <c02_receita>100099</c02_receita>
                            <c26_produto>20</c26_produto>
                            <c27_tipoIdentificacaoEmitente>1</c27_tipoIdentificacaoEmitente>
                            <c03_idContribuinteEmitente><CNPJ>XXXXXX</CNPJ></c03_idContribuinteEmitente>
                            <c06_valorPrincipal>0.01</c06_valorPrincipal>
                            <c14_dataVencimento>2015-06-05</c14_dataVencimento>
                            <c15_convenio>49-Protocolo</c15_convenio>
                            <c16_razaoSocialEmitente>XXXXXX</c16_razaoSocialEmitente>
                            <c18_enderecoEmitente>XXXXXX</c18_enderecoEmitente>
                            <c19_municipioEmitente>50308</c19_municipioEmitente>
                            <c20_ufEnderecoEmitente>SP</c20_ufEnderecoEmitente>
                            <c21_cepEmitente>XXXXXX</c21_cepEmitente>
                            <c22_telefoneEmitente>XXXXXX</c22_telefoneEmitente>
                            <c34_tipoIdentificacaoDestinatario>1</c34_tipoIdentificacaoDestinatario>
                            <c35_idContribuinteDestinatario><CNPJ>XXXXXXXXXX</CNPJ></c35_idContribuinteDestinatario>
                            <c37_razaoSocialDestinatario>XXXXXXXXXXXXXXXXX</c37_razaoSocialDestinatario>
                            <c38_municipioDestinatario>00054</c38_municipioDestinatario>
                            <c33_dataPagamento>2015-06-05</c33_dataPagamento>
                            <c05_referencia><mes>06</mes><ano>2015</ano></c05_referencia>
                            <c39_camposExtras>
                            <campoExtra>
                            <codigo>9</codigo><tipo>T</tipo><valor>XXXXXXXXXXXXXXXXXXXXXXXX</valor>
                            </campoExtra>
                            </c39_camposExtras>
                            </TDadosGNRE></guias></TLote_GNRE></gnreDadosMsg>
                            </soap:Body>
                            </soap:Envelope>";

In the request use the following code:

 var webRequest = (HttpWebRequest)WebRequest.Create(Mensagens.WebServiceLoteRecepcao);

            webRequest.Method = "POST";
            webRequest.Headers.Add(string.Format("SOAPAction:{0}",Mensagens.SoapActionRecepcao));
            webRequest.ContentType = "text/xml; charset=utf-8";
            webRequest.Credentials = CredentialCache.DefaultCredentials;
            webRequest.ClientCertificates.Add(_certificado);

Whereas:

Mensagens.SoapActionRecepcao = "http://www.gnre.pe.gov.br/wsdl/processar"
Mensagens.WebServiceLoteRecepcao = "https://www.gnre.pe.gov.br/gnreWS/services/GnreLoteRecepcao"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.