oribeiro Posted August 19, 2022 Report Share Posted August 19, 2022 Pessoal, Eu estou programando a consulta no WebService para obtenção de informações do GTIN mas não estou acertando. Por favor, vejam o código abaixo e, se possível, me informem onde eu estou errando. Obrigado. /////// // CONSULTA WEBSERVICE: GTIN // #include "FiveWin.ch" function Main() local oWnd, oBar, oWebServer DEFINE WINDOW oWnd TITLE "Web Service" DEFINE BUTTONBAR oBar BUTTONSIZE 60,30 OF oWnd _3D DEFINE BUTTON OF oBar PROMPT "&Execute" ACTION ConectarSOAP() DEFINE BUTTON OF oBar PROMPT "&Sair" ACTION oWnd:End() ACTIVATE WINDOW oWnd return nil //----------------------------------------------------------------------------// Static Function ConectarSOAP() // Função para consumir WebService #Include "Common.ch" // fwh\include #Include "winapi.ch" // fwh\include #Include "objects.ch" // fwh\include #include "hbXml.ch" // fwh\include // Variáveis utilizadas // Local doc, http, cXmlBody, cFilename, oText, hFile, xmldoc, xmlnote, cnome, cxmlretorno, xmlnode, response CursorWait() // Abertura das funções para WebService do xHarbour // doc = CreateObject( "MSXML2.DOMDocument" ) http = CreateObject( "MSXML2.XMLHTTP" ) // Funciona também -> "Microsoft.XMLHTTP" /////////////////////////////////////////////////////////////////// ** // 1 - Aqui você coloca o endereço do WebService a ser consumido // /////////////////////////////////////////////////////////////////// // Exemplo: // http:Open( "POST" , "http://www.conectapdv.com.br/concentrador/concentrador.asmx?wsdl" , .f. ) http:Open( "POST" , "https://dfe-servico.svrs.rs.gov.br/ws/ccgConsGTIN/ccgConsGTIN.asmx" , .f. ) /////////////////////////////////////////////////////////////////// ** // 2 - Aqui você coloca o seu XML com a requisição = request // /////////////////////////////////////////////////////////////////// // Exemplo: // cXMlBody = '<?xml version="1.0" encoding="UTF-8"?>' + ; // '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="http://dfe-servico.svrs.rs.gov.br/">'+; // ' <SOAP-ENV:Body>'+ ; // ' <WS_Busc_Projetos xmlns="http://tempuri.org/">'+; // ' <cIdentifica><identifica><cnpj>11222333000181</cnpj><terminal>020012</terminal><autentica>51131212</autentica></identifica></cIdentifica>'+; // ' </WS_Busc_Projetos>'+; // ' </SOAP-ENV:Body>'+; // '</SOAP-ENV:Envelope>' cXMlBody = ''+; '<?xml version="1.0" encoding="UTF-8"?>'+; '<xs:schema xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns="http://www.portalfiscal.inf.br/nfe" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.portalfiscal.inf.br/nfe" elementFormDefault="qualified" attributeFormDefault="unqualified">'+; ' <xs:include schemaLocation="tiposBasicosCcgConsGTIN_v1.00.xsd"/>'+; ' <xs:complexType name="TConsGTIN">'+ ; ' <xs:annotation>'+; ' <xs:documentation>Tipo Consulta pública Cadastro Centralizado de GTIN</xs:documentation>'+; ' <xs:annotation>'+; ' <xs:sequence>'+; ' <xs:element name="GTIN" type="TCodGTIN">'+; ' <xs:GTIN>7894900019896</xs:GTIN>'+ ; ' </xs:element>'+; ' </xs:sequence>'+; ' <xs:attribute name="versao" type="TVerConsGTIN" use="required"/>'+; ' </xs:complexType>'+; '</xs:schema>' /* cXMlBody = ''+; '<?xml version="1.0" encoding="UTF-8"?>'+; '<xs:schema xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns="http://www.portalfiscal.inf.br/nfe" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.portalfiscal.inf.br/nfe" elementFormDefault="qualified" attributeFormDefault="unqualified">'+; ' <xs:include schemaLocation="tiposBasicosCcgConsGTIN_v1.00.xsd"/>'+; ' <xs:complexType name="TConsGTIN">'+; ' <xs:annotation>'+; ' <xs:documentation>Tipo Consulta pública Cadastro Centralizado de GTIN</xs:documentation>'+; ' </xs:annotation>'+; ' <xs:sequence>'+; ' <xs:element name="GTIN" type="TCodGTIN">'+; ' <xs:annotation>'+; ' <xs:documentation>Informar o código GTIN a ser consultado.</xs:documentation>'+; ' </xs:annotation>'+; ' </xs:element>'+; ' </xs:sequence>'+; ' <xs:attribute name="versao" type="TVerConsGTIN" use="required"/>'+; ' </xs:complexType>'+; ' <xs:complexType name="TRetConsGTIN">'+; ' <xs:annotation>'+; ' <xs:documentation>Tipo Retorno da Consulta pública Cadastro Centralizado de GTIN</xs:documentation>'+; ' </xs:annotation>'+; ' <xs:sequence>'+; ' <xs:element name="verAplic" type="TVerAplic">'+; ' <xs:annotation>'+; ' <xs:documentation>Versão da aplicação da SVRS.</xs:documentation>'+; ' </xs:annotation>'+; ' </xs:element>'+; ' <xs:element name="cStat" type="TStat">'+; ' <xs:annotation>'+; ' <xs:documentation>Código do Status da resposta (resultado do processamento do lote).</xs:documentation>'+; ' </xs:annotation>'+; ' </xs:element>'+; ' <xs:element name="xMotivo" type="TMotivo">'+; ' <xs:annotation>'+; ' <xs:documentation>Descrição literal do Status da resposta (resultado do processamento do lote).</xs:documentation>'+; ' </xs:annotation>'+; ' </xs:element>'+; ' <xs:element name="dhResp" type="TDateTimeUTC">'+; ' <xs:annotation>'+; ' <xs:documentation>Data e hora da resposta, no formato AAAA-MM-DDThh:mm:ssTZD (UTC - Universal Coordinated Time).</xs:documentation>'+; ' </xs:annotation>'+; ' </xs:element>'+; ' <xs:element name="GTIN" type="TCodGTIN" minOccurs="0">'+; ' <xs:annotation>'+; ' <xs:documentation>(idem mensagem de entrada) Código GTIN.</xs:documentation>'+; ' </xs:annotation>'+; ' </xs:element>'+; ' <xs:element name="tpGTIN" type="TTpGTIN" minOccurs="0">'+; ' <xs:annotation>'+; ' <xs:documentation>Tipo do GTIN, quantidade de dígitos (8, 12, 13 ou 14).</xs:documentation>'+; ' </xs:annotation>'+; ' </xs:element>'+; ' <xs:element name="xProd" minOccurs="0">'+; ' <xs:annotation>'+; ' <xs:documentation>Descrição do produto.</xs:documentation>'+; ' </xs:annotation>'+; ' <xs:simpleType>'+; ' <xs:restriction base="TString">'+; ' <xs:minLength value="2"/>'+; ' <xs:maxLength value="500"/>'+; ' </xs:restriction>'+; ' </xs:simpleType>'+; ' </xs:element>'+; ' <xs:element name="NCM" minOccurs="0">'+; ' <xs:annotation>'+; ' <xs:documentation>Código do NCM (8 posições).</xs:documentation>'+; ' </xs:annotation>'+; ' <xs:simpleType>'+; ' <xs:restriction base="TString">'+; ' <xs:pattern value="[0-9]{8}"/>'+; ' </xs:restriction>'+; ' </xs:simpleType>'+; ' </xs:element>'+; ' <xs:element name="CEST" minOccurs="0" maxOccurs="3">'+; ' <xs:annotation>'+; ' <xs:documentation>Código Especificador da Substituição Tributária - CEST, que identifica a mercadoria sujeita aos regimes de substituição tributária e de antecipação do recolhimento do imposto.</xs:documentation>'+; ' </xs:annotation>'+; ' <xs:simpleType>'+; ' <xs:restriction base="TString">'+; ' <xs:pattern value="[0-9]{7}"/>'+; ' </xs:restriction>'+; ' </xs:simpleType>'+; ' </xs:element>'+; ' </xs:sequence>'+; ' <xs:attribute name="versao" type="TVerConsGTIN" use="required"/>'+; ' </xs:complexType>'+; ' <xs:simpleType name="TVerConsGTIN">'+; ' <xs:annotation>'+; ' <xs:documentation>Tipo Versão do Leiaute para Consulta Pública GTIN</xs:documentation>'+; ' </xs:annotation>'+; ' <xs:restriction base="xs:token">'+; ' <xs:pattern value="1\.00"/>'+; ' </xs:restriction>'+; ' </xs:simpleType>'+; '</xs:schema>' */ /////////////////////////////////////////////////////////////////// ** // 3 - Aqui você coloca a ação que o WebService deve executar // /////////////////////////////////////////////////////////////////// // Exemplo: // http:SetRequestHeader( "SOAPAction" , "http://tempuri.org/WS_Busc_Projetos" ) // http:SetRequestHeader( "Content-Type" , "text/xml" ) // Funções do WebService // doc:LoadXML( cXmlBody ) // Carrega o request http:Send( doc:xml ) // Envia o request response = http:responseText // Recebe o response cFilename:="texto.xml" // Nome do arquivo temporário FErase(cfilename) // Apaga o arquivo, caso exista //Cria o arquivo temporário e armazena o XML retornado oText:= TTxtFile():New( cfilename ) if oText:Open() oText:add(alltrim(response)) endif oText:close() // Mostra o XML retornado // ? "XML RETORNADO:",,response // Abre o arquivo temporário hFile := FOpen( cFileName ) IF hFile == -1 MsgStop("Erro ao abrir arquivo:"+cFileName) Return Nil ENDIF // Pega o conteudo do arquivo temporário pela classe TxmlDocument() // xmlDoc := TXmlDocument():New( hFile ) /////////////////////////////////////////////////////////////////// ** // 4 - Captura os dados do nó principal "WS_Busc_ProjetosResult" // // que deverá ser substituído pelo seu. // /////////////////////////////////////////////////////////////////// xmlNode := xmlDoc:oRoot:oChild DO WHILE xmlNode != NIL cNome:=xmlNode:cName if cNome=='WS_Busc_ProjetosResult' cXMLRetorno:=xmlNode:cData endif xmlNode := xmlNode:NextInTree() ENDDO fclose(hFile) // Fecha o arquivo temporário // Mostra o conteudo do nó principal // ? "RETORNO DA CONSULTA:",,cXMLRetorno Return nil Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.