GNOME WebService Integration with C #

0

Can anyone help me or have you ever had this problem?

I'm doing a project to integrate my software with GNRE's webservice to send the guides automated.

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

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

I was looking at some posts and they told me that it could also be a Firewal block.

Detail, I already have the company's digital certificate 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'm 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;
    }
  }                 
}
    
asked by anonymous 18.06.2015 / 16:03

2 answers

1

The error 500 Internal Server Error occurs when:

  • Failed to configure the server
  • A server dependency has stopped working
  • Instructions sent incorrectly to the headers or to the body of the content

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 body of the content ), like for example:

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

I recommend that you correct this.

One way to send SOAP with c # (csharp) is to use something like this in 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);
    }
}
    
18.06.2015 / 20:56
-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);

Being that:

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