1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

  2. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

WebService Sefaz - Consulta de CNPJ

Discussão em 'Progress 4GL' iniciado por kikofr, Outubro 6, 2009.

  1. kikofr

    kikofr Membro Participativo

    Bom dia,

    Alguem já usou o webservice da Sefaz para consulta de CNPJ?

    Abaixo o endereço que tenho para acessar o webservice (detalhe, não achei o wsdl) dele.
    Código:
    http://webservices.sefaz.rs.gov.br/CadastroContribuintesRSGeral_HTML.asp?IE="&wIE&"&cnpj="&wcnpj&"&cpf="&wcpf&"&cpftitular="&wcpftitular&"&senha="&wsenha
    Como posso utilizar esse webservice sem precisar abrir um navegador e utilizar as variaveis dele dentro do progress?

    Obrigado desde já.
  2. fdantas

    fdantas Administrator Moderador

    Fala ae Kiko tudo bom ?

    Kiko encontrei um exemplo em vb utilizando o webservice citado...
    Se quiser desenvolver algo open pra colocarmos aqui no forum eu ajudo....

    Abraço

    Código:
    -------------------------------------
    ' Exemplo de programa escrito em Visual Basic que acessa um
    ' Web Service da Secretaria da Fazenda do RS que disponibiliza
    ' informações de contribuintes do Estado.
    ' ---------------------------------------------------------
    ' Requisito :
    ' Adicionar no projeto VB a referência ao componente
    ' Microsoft XML v2.6
    ' Como funciona :
    ' O programa passa os parâmetros de pesquisa através de
    ' uma estrutura de documento XML definido para o Web Service
    ' em questão.
    '
    
    Private Sub Command1_Click()
    On Error GoTo TrataErro
    Dim objXMLHTTP As New MSXML2.XMLHTTP
    Dim objXMLDOM As New MSXML2.DOMDocument26
    Dim sConteudo As String
    Dim sIE As String
    Dim sCNPJ As String
    Dim sCategoria As String
    Dim sCNAE As String
    Dim sUF As String
    Dim sSituacao As String
    Dim sDataInicio As String
    Dim sDataBaixa As String
    Dim sStatus As String
    Dim sMsgErro As String
    Dim sCPFtitular As String
    Dim sTipoEstab As String
    Dim sQuantidade As String
    Dim sTipoPessoa As String
    Dim sVinculo As String
    
    If Trim(txtCPF) = "" Then
    MsgBox "Informe o CPF do usuário!", vbExclamation + vbOKOnly, "Atenção"
    txtCPF.SetFocus
    Exit Sub
    End If
    If Trim(txtSenha) = "" Then
    MsgBox "Informe a Senha do usuário!", vbExclamation + vbOKOnly, "Atenção"
    txtSenha.SetFocus
    Exit Sub
    End If
    ' Informar apenas um dos 3 campos
    If Trim(txtIE) <> "" And Trim(txtCNPJ) <> "" And Trim(txtCPFtitular) <> "" Or _
    Trim(txtIE) <> "" And Trim(txtCNPJ) <> "" Or _
    Trim(txtIE) <> "" And Trim(txtCPFtitular) <> "" Or _
    Trim(txtCNPJ) <> "" And Trim(txtCPFtitular) <> "" Then
    MsgBox "Informe somente a IE, ou somente o CNPJ, ou somente o CPF do titular.", vbExclamation + vbOKOnly, "Atenção"
    txtCPFtitular.SetFocus
    Exit Sub
    End If
    
    '------------------------------------------------------'
    ' 1ª parte - Envia um documento XML para o Web Service '
    '------------------------------------------------------'
    
    'Monta o documento XML conforme a estrutura definida para este Web Service
    sConteudo = ""
    sConteudo = sConteudo & "<PARAMETROS>"
    sConteudo = sConteudo & " <CPF>" & Right("00000000000" & Trim(txtCPF), 11) & "</CPF>"
    sConteudo = sConteudo & " <SENHA>" & txtSenha & "</SENHA>"
    sConteudo = sConteudo & " <IE>" & Right("0000000000" & Trim(txtIE), 10) & "</IE>"
    sConteudo = sConteudo & " <CNPJ>" & Right("00000000000000" & Trim(txtCNPJ), 14) & "</CNPJ>"
    sConteudo = sConteudo & " <CPFTITULAR>" & Right("00000000000" & Trim(txtCPFtitular), 11) & "</CPFTITULAR>"
    sConteudo = sConteudo & "</PARAMETROS>"
    
    'Converte para um documento XML no padrão DOM
    objXMLDOM.async = False
    objXMLDOM.loadXML (sConteudo)
    'Aponta para o Web Service
    objXMLHTTP.open "POST", "http://webservices.sefaz.rs.gov.br/CadastroContribuintesRSGeral_XML.asp", False
    'Envia o documento XML para o Web Service
    objXMLHTTP.send (objXMLDOM.xml)
    
    '-----------------------------------------'
    ' 2ª parte - Recebe um XML do Web Service '
    '-----------------------------------------'
    Dim sRetorno As String
    Dim objNodes As MSXML2.IXMLDOMNodeList
    Dim objBookNode As MSXML2.IXMLDOMNode
    Dim objBookNodeEMP As MSXML2.IXMLDOMNode
    Set objNodes = objXMLDOM.selectNodes("PARAMETROS")
    Set objNodesEMP = objXMLDOM.selectNodes("PARAMETROS/EMPRESA")
    Dim aOCORRENCIA(5) As String
    Dim aIEDUP(5) As String
    Dim aNOMEDUP(5) As String
    Dim aSITDUP(5) As String
    Dim aTIPODUP(5) As String
    Dim aVINCULODUP(5) As String
    Dim ind As Integer
    
    ind = 0
    
    'Aqui é carregado o retorno do Web Service.
    sRetorno = objXMLHTTP.responseXML.xml
    objXMLDOM.async = False
    objXMLDOM.loadXML (sRetorno)
    
    'Percorre o documento XML recebido do Web Service
    For Each objBookNode In objNodes
    If objBookNode.selectNodes("IE").length <> 0 Then
    sIE = objBookNode.selectSingleNode("IE").nodeTypedValue
    End If
    If objBookNode.selectNodes("CNPJ").length <> 0 Then
    sCNPJ = objBookNode.selectSingleNode("CNPJ").nodeTypedValue
    End If
    If objBookNode.selectNodes("CPFTITULAR").length <> 0 Then
    sCPFtitular = objBookNode.selectSingleNode("CPFTITULAR").nodeTypedValue
    End If
    If objBookNode.selectNodes("CATEGORIA").length <> 0 Then
    sCategoria = objBookNode.selectSingleNode("CATEGORIA").nodeTypedValue
    End If
    If objBookNode.selectNodes("DATAINICIO").length <> 0 Then
    sDataInicio = objBookNode.selectSingleNode("DATAINICIO").nodeTypedValue
    End If
    If objBookNode.selectNodes("DATABAIXA").length <> 0 Then
    sDataBaixa = objBookNode.selectSingleNode("DATABAIXA").nodeTypedValue
    End If
    If objBookNode.selectNodes("SITUACAO").length <> 0 Then
    sSituacao = objBookNode.selectSingleNode("SITUACAO").nodeTypedValue
    End If
    If objBookNode.selectNodes("TIPOESTAB").length <> 0 Then
    sTipoEstab = objBookNode.selectSingleNode("TIPOESTAB").nodeTypedValue
    End If
    If objBookNode.selectNodes("QUANTIDADE").length <> 0 Then
    sQuantidade = objBookNode.selectSingleNode("QUANTIDADE").nodeTypedValue
    End If
    If objBookNode.selectNodes("TIPOPESSOA").length <> 0 Then
    sTipoPessoa = objBookNode.selectSingleNode("TIPOPESSOA").nodeTypedValue
    End If
    If objBookNode.selectNodes("VINCULO").length <> 0 Then
    sVinculo = objBookNode.selectSingleNode("VINCULO").nodeTypedValue
    End If
    If objBookNode.selectNodes("CNAE").length <> 0 Then
    sCNAE = objBookNode.selectSingleNode("CNAE").nodeTypedValue
    End If
    If objBookNode.selectNodes("UF").length <> 0 Then
    sUF = objBookNode.selectSingleNode("UF").nodeTypedValue
    End If
    If objBookNode.selectNodes("STATUS").length <> 0 Then
    sStatus = objBookNode.selectSingleNode("STATUS").nodeTypedValue
    End If
    If objBookNode.selectNodes("MSGERRO").length <> 0 Then
    sMsgErro = objBookNode.selectSingleNode("MSGERRO").nodeTypedValue
    End If
    If objBookNode.selectNodes("EMPRESA").length <> 0 Then
    For Each objBookNodeEMP In objNodesEMP
    ind = ind + 1
    If ind < 6 Then 'Só mostraremos os 5 primeiros estabelecimentos
    If objBookNodeEMP.selectNodes("IEDUP").length <> 0 Then
    aIEDUP(ind) = objBookNodeEMP.selectSingleNode("IEDUP").nodeTypedValue
    End If
    If objBookNodeEMP.selectNodes("NOMEDUP").length <> 0 Then
    aNOMEDUP(ind) = objBookNodeEMP.selectSingleNode("NOMEDUP").nodeTypedValue
    End If
    If objBookNodeEMP.selectNodes("SITDUP").length <> 0 Then
    aSITDUP(ind) = objBookNodeEMP.selectSingleNode("SITDUP").nodeTypedValue
    End If
    If objBookNodeEMP.selectNodes("TIPODUP").length <> 0 Then
    aTIPODUP(ind) = objBookNodeEMP.selectSingleNode("TIPODUP").nodeTypedValue
    End If
    If objBookNodeEMP.selectNodes("VINCULODUP").length <> 0 Then
    aVINCULODUP(ind) = objBookNodeEMP.selectSingleNode("VINCULODUP").nodeTypedValue
    End If
    End If
    Next
    End If
    Next objBookNode
    
    If sStatus = "01" Or sStatus = "02" Then
    ' Este Web Service retorna na variável Status:
    ' 01 - para inconsistência nos dados digitados
    ' 02 - para erro de comunicação com o Web Service
    txtResult = IIf(sStatus = "01", "INCONSISTÊNCIA: ", "ERRO: ") & sMsgErro
    Else
    If sTipoEstab <> "DUPLO" Then
    ' Apresenta os dados da empresa recebidos do Web Service
    txtResult = ""
    txtResult = txtResult & "IE: " & Mid(sIE, 1, 3) & "/" & Mid(sIE, 4) & vbNewLine
    txtResult = txtResult & "CNPJ: " & Mid(sCNPJ, 1, 2) & "." & _
    Mid(sCNPJ, 3, 3) & "." & _
    Mid(sCNPJ, 6, 3) & "/" & _
    Mid(sCNPJ, 9, 4) & "-" & _
    Mid(sCNPJ, 13, 2) & vbNewLine
    txtResult = txtResult & "CPF do Titular: " & Mid(sCPFtitular, 1, 3) & "." & _
    Mid(sCPFtitular, 4, 3) & "." & _
    Mid(sCPFtitular, 7, 3) & "-" & _
    Mid(sCPFtitular, 10, 2) & vbNewLine
    txtResult = txtResult & "Quantidade: " & sQuantidade & vbNewLine
    txtResult = txtResult & "Tipo de Estabelecimento: " & sTipoEstab & vbNewLine
    txtResult = txtResult & "Tipo de Pessoa: " & IIf(sTipoPessoa = "J", "JURÍDICA", IIf(sTipoPessoa = "F", "FÍSICA", "")) & vbNewLine
    txtResult = txtResult & "Categoria: " & sCategoria & vbNewLine
    txtResult = txtResult & "CNAE-Fiscal: " & sCNAE & vbNewLine
    txtResult = txtResult & "UF: " & sUF & vbNewLine
    txtResult = txtResult & "Tipo de Vínculo: " & sVinculo & vbNewLine
    txtResult = txtResult & "Situação: " & sSituacao & vbNewLine
    txtResult = txtResult & "Data de início: " & sDataInicio & vbNewLine
    txtResult = txtResult & "Data da baixa: " & sDataBaixa
    DoEvents
    Else
    ' Apresenta os dados da empresa recebidos do Web Service
    txtResult = ""
    txtResult = txtResult & "IE: " & Mid(sIE, 1, 3) & "/" & Mid(sIE, 4) & vbNewLine
    txtResult = txtResult & "CNPJ: " & Mid(sCNPJ, 1, 2) & "." & _
    Mid(sCNPJ, 3, 3) & "." & _
    Mid(sCNPJ, 6, 3) & "/" & _
    Mid(sCNPJ, 9, 4) & "-" & _
    Mid(sCNPJ, 13, 2) & vbNewLine
    txtResult = txtResult & "CPF do Titular: " & Mid(sCPFtitular, 1, 3) & "." & _
    Mid(sCPFtitular, 4, 3) & "." & _
    Mid(sCPFtitular, 7, 3) & "-" & _
    Mid(sCPFtitular, 10, 2) & vbNewLine
    txtResult = txtResult & "Tipo de Estabelecimento: " & sTipoEstab & vbNewLine
    txtResult = txtResult & "Quantidade: " & sQuantidade & vbNewLine
    For f = 1 To ind
    If f > 5 Then 'Só mostraremos os 5 primeiros estabelecimentos
    Exit For
    End If
    txtResult = txtResult & "-(" & f & ")" & String(30, "-") & vbNewLine
    txtResult = txtResult & "IE: " & Mid(aIEDUP(f), 1, 3) & "/" & Mid(aIEDUP(f), 4) & vbNewLine
    txtResult = txtResult & "Nome: " & aNOMEDUP(f) & vbNewLine
    txtResult = txtResult & "Situação: " & IIf(aSITDUP(f) = "A", "ATIVA", IIf(aSITDUP(f) = "B", "BAIXADA", "")) & vbNewLine
    txtResult = txtResult & "Tipo de Estabelecimento: " & aTIPODUP(f) & vbNewLine
    txtResult = txtResult & "Tipo de Vínculo: " & aVINCULODUP(f) & vbNewLine
    Next
    End If
    End If
    'Seleciona o campo que foi preenchido na última consulta
    If Trim(txtIE) <> "" Then
    txtIE.SelStart = 0
    txtIE.SelLength = 10
    txtIE.SetFocus
    Else
    If Trim(txtCNPJ) <> "" Then
    txtCNPJ.SelStart = 0
    txtCNPJ.SelLength = 14
    txtCNPJ.SetFocus
    Else
    txtCPFtitular.SelStart = 0
    txtCPFtitular.SelLength = 14
    txtCPFtitular.SetFocus
    End If
    End If
    Exit Sub
    
    TrataErro:
    MsgBox Err.Description
    End
    End Sub
    
    Private Sub Command2_Click()
    End
    End Sub
    
    Private Sub txtCPFtitular_Change()
    Me.txtResult = ""
    End Sub
    
    Private Sub txtIE_Change()
    Me.txtResult = ""
    End Sub
    
    Private Sub txtCNPJ_Change()
    Me.txtResult = ""
    End Sub
    
    Private Sub txtCPF_Change()
    Me.txtResult = ""
    End Sub
    
    Private Sub txtSenha_Change()
    Me.txtResult = ""
    End Sub
    
  3. kikofr

    kikofr Membro Participativo

    A minha idéia é criar um código open para fazer isso....

    O que estava pensando, como não estou conseguindo acessar diretamente o webservice pois não tem WSDL, estava pensando em mandar via HTML a solicitação e ele irá me retornar o código XML em formato String. Depois converter para XML... será que da para fazer alguma coisa assim?

    Como faço para executar um request para o site?

    mais ou menos como no exemplo em VB.
    Código:
    objXMLHTTP.open "POST", "http://webservices.sefaz.rs.gov.br/CadastroContribuintesRSGeral_XML.asp", False
    'Envia o documento XML para o Web Service
    objXMLHTTP.send (objXMLDOM.xml)
    
  4. fdantas

    fdantas Administrator Moderador

  5. kikofr

    kikofr Membro Participativo

    Fábio,

    Sim, mas tem como eu fazer um request em progress para o site? enviando os parametros e ele me retornando a string com o resultado?
  6. fdantas

    fdantas Administrator Moderador

    Daria pra usar essa pagina em asp q eles disponibilizam junto com webspeed.... ai acho que ficaria facil...

Compartilhe esta Página