//CODIGO FUENTE PASO 1 ES UN SIMPLE PROGRAMA MD203 QUE CREA UN XML Y LO ENVIA A UN WS,, EL CUAL RETORNA UN ID Y UN TOKEN Y ACONTINUACION ESTA EL MD204 QUE ES DONDE TENGO EL PROBLEMA,,, POR LO QUE PUEDO APRECIAR SE DEBERIA INSTANCIAR UN OBJETO...
include "Fivewin.ch"
include "Xml.ch"
include "hbxml.ch"
include "dll.ch"
include "hbcurl.ch"
FUNCTION MD203() //Obtener Token Web Service
LOCAL oHttp,Doc,cxmLbody,oIni,oDlg2,oSay,oError,oInternet
LOCAL ts_token:="",ts_usuario:=space(50),ts_id:=space(50)
LOCAL ts_clave:=space(20),ts_exit:=.f.,tw_servidor:=''
LOCAL nrate:=nil
if !isinternet()
msgalert('Computador Sin Conexion a Internet','Verifique')
return .f.
endif
if .NOT.FILE("\MLMALL\config\config.ini")
msgalert('Archivo de Configuracion, No Existe...','Verifique')
return .f.
else
INI oIni File "\MLMALL\config\config.ini"
GET tw_servidor SECTION "WEBSERVICE" ENTRY "Servidor" OF oIni
GET ts_token SECTION "TOKEN SESION "+e_rut ENTRY "Token" OF oIni
GET ts_usuario SECTION "TOKEN SESION "+e_rut ENTRY "Usuario" OF oIni
GET ts_id SECTION "TOKEN SESION "+e_rut ENTRY "Id" OF oIni
endif
if empty(ts_token)
msginfo('Debe Obtener un Token de Inicio para Autentificarse en Web Service','Token de Sesion')
endif
if empty(ts_usuario)
ts_usuario:=space(50)
else
ls_largo:=len(ts_usuario)
if ls_largo<50
ts_usuario:=ts_usuario+space(50-ls_largo)
endif
endif
ts_servidor:=tw_servidor
DEFINE DIALOG oDlg2 RESOURCE "MOD203" TITLE oemtoansi("Autentificaci垄n e Inicio de Sesi垄n WEBSERVICE")
REDEFINE SAY oSay VAR ts_token ID 4002 OF odlg2 COLOR rgb(0,0,255) UPDATE
REDEFINE SAY oSay VAR ts_servidor ID 4004 OF odlg2 COLOR rgb(0,0,255) UPDATE
REDEFINE SAY oSay VAR ts_id ID 4006 OF odlg2 COLOR rgb(0,0,255) UPDATE
REDEFINE GET ts_usuario ID 4012 OF odlg2 UPDATE
REDEFINE GET ts_clave ID 4014 OF odlg2 UPDATE
REDEFINE BUTTON ID 4009 OF odlg2 ACTION(ts_exit:=.t.,oDlg2:end())
REDEFINE BUTTON ID 4010 OF odlg2 ACTION(oDlg2:end())
ACTIVATE DIALOG oDlg2 CENTERED
if ts_exit
cXmlBody := '' + ;
'<?xml version="1.0" encoding="UTF-8"?>' + ;
'<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:tem="http://tempuri.org/">'+;
' <soap:Header xmlns:wsa="http://www.w3.org/2005/08/addressing"><wsa:Action>http://tempuri.org/IWcfCP/Autenticar</wsa:Action><wsa:To>https://zofri.mekanosige.cl:2414/WcfCP.svc</wsa:To></soap:Header>'+;
' <soap:Body>'+;
' <tem:Autenticar>'+;
' <tem:User>'+alltrim(ts_usuario)+'</tem:User>'+;
' <tem:Pass>'+alltrim(ts_clave)+'</tem:Pass>'+;
' </tem:Autenticar>'+;
' </soap:Body>'+;
'</soap:Envelope>'
//verifica si esta activo el servidor
doc = CreateObject( "MSXML2.DOMDocument" )
ohttp=CreateObject( "Microsoft.XMLHTTP")
ohttp:Open( "POST" , ts_servidor+' HTTP/1.1',.f. )
ohttp:SetRequestHeader( "Accept-Encoding","gzip,deflate")
ohttp:SetRequestHeader( "Content-Type" , "application/soap+xml;charset=UTF-8" )
ohttp:SetRequestHeader( "SOAPAction" , "http://tempuri.org/IWcfCP/Autenticar" )
ohttp:SetRequestHeader( "User-Agent" , "Jakarta Commons-HttpClient/3.1;Apache-HttpClient/4.1.1 (java 1.5)" )
ohttp:setrequestHeader( "Host", "https://zofri.mekanosige.cl:2414" )
ohttp:SetRequestHeader( "Connection" , "Keep-Alive" )
doc:LoadXML( cXmlBody )
TRY
ohttp:Send( doc:xml )
CATCH
cErrorMsg:='No Se Pudo Enviar Documento XML...'+CRLF+CRLF+;
'Sin Conexion a Internet o Servidor Con Problemas'
msginfo(cErrorMsg,"Intente Nuevamente")
return .t.
END
cresponse=ohttp:responseText
If .Not. Empty( Alltrim( cresponse ) )
buscar1:=AT("Id",cResponse)
if buscar1#0
cresponse:=substr(cresponse,buscar1)
buscarA:=AT(";",cResponse)
buscarB:=AT("<",cResponse)
len_ab:=buscarB-buscarA
ts_id:=substr(cresponse,buscarA+1,len_ab-1)
buscar1:=AT("Token",cResponse)
cresponse:=substr(cresponse,buscar1)
buscarA:=AT("gt;",cResponse)
buscarB:=AT("&lt",cResponse)
len_ab:=buscarB-buscarA
ts_token:=substr(cresponse,buscarA+3,len_ab-3)
cRetorno=oemtoansi("Comunicaci垄n Establecida Con Servidor")
else
ts_token:=""
cRetorno = "### Hubo un Problema Con El Servidor, Mensaje No Pudo Ser Entregado ###"+CRLF+;
"### Nombre de Usuario Incorrecto ###"+CRLF+;
oemtoansi("### Contrase陇a Incorrecta ###")+CRLF+;
"### Servidor Fuera de Linea ###"
endif
if .not.empty(alltrim(ts_token))
INI oIni File "\MLMALL\config\config.ini"
SET SECTION "TOKEN SESION "+e_rut ENTRY "Token" OF oIni TO ts_token
SET SECTION "TOKEN SESION "+e_rut ENTRY "Id" OF oIni TO ts_id
SET SECTION "TOKEN SESION "+e_rut ENTRY "Usuario" OF oIni TO ts_usuario
SET SECTION "WEBSERVICE" ENTRY "Servidor" OF oIni TO tw_servidor
MD204()
endif
else
cRetorno = "### No Fue Posible Generar Proceso ###"
Endif
msginfo(cretorno,'Resultado del Proceso')
Release doc
Release ohttp
Release res
Release response
endif
Return .t.
FUNCTION md204()
LOCAL oHttp,Doc,cxmLbody2,oIni,oError,oInternet
LOCAL PathCert:="\mlmall\config\pos\zofri.pfx"
LOCAL PassCert:="Ah2Cfg723rQ"
LOCAL cDllname:="\MLMall\Config\POS\MkSg.InterfazPOS.Security.dll"
LOCAL ts_token:="",cXlmBody:="",tw_servidor:=""
LOCAL clObj
LOCAL ts_cliente:='1547',ts_local:='1032',ts_caja:='001',ts_tipo:='BO'
LOCAL ts_mod:='M',ts_folio:='12345'
LOCAL ts_tbruto:=15000,ts_tneto:=13000,ts_timp:=2000
hlib := loadlibrary(cDllName)
if abs(hlib)<=32
msgAlert ("Error Code: "+Ltrim(str(hlib))+" Loading "+ cDllName )
return .t.
endif
if .NOT.FILE("\MLMALL\config\config.ini")
msgalert('Archivo de Configuracion, No Existe...','Verifique')
return .f.
else
INI oIni File "\MLMALL\config\config.ini"
GET ts_token SECTION "TOKEN SESION "+e_rut ENTRY "Token" OF oIni
GET tw_servidor SECTION "WEBSERVICE" ENTRY "Servidor" OF oIni
endif
if empty(ts_token)
msginfo('Debe Obtener un Token de Inicio para Autentificarse en Web Service','Token de Sesion')
endif
// cSecurity_csec:=csecurity():new()
// clObj:=CreateObject("cSecurity")
// clObj:=Createobject(c_security(pathCert,passCert))
// oSvr:=Createobject("cSecurity")
// oSvr:create:new()
// cSecurity_csec:=c_security():new(pathCert,passCert)
//Entregar a la clase ruta y pass del certificado
cSecurity_csec:=c_security(pathCert,passCert)
//Crear XML con Datos
cXmlBody := '' + ;
'<Transaccion>'+;
' <CodCliente>'+alltrim(ts_cliente)+'</CodCliente>'+;
' <CodLocal>'+alltrim(ts_local)+'</CodLocal>'+;
' <CodCaja>'+alltrim(ts_caja)+'</CodCaja>'+;
' <FecTrx>'+dtoc(date())+' '+time()+'</FecTrx>'+;
' <TipDoc>'+alltrim(ts_tipo)+'</TipDoc>'+;
' <ModDoc>'+alltrim(ts_mod)+'</ModDoc>'+;
' <Folio>'+alltrim(ts_folio)+'</FecTrx>'+;
' <Totales>'+;
' <TotBruto>'+transform(ts_tbruto,'##########')+'</TipDoc>'+;
' <TotNeto>'+transform(ts_tneto,'##########')+'</TotNet>'+;
' <TotImp>'+transform(ts_timp,'##########')+'</TotImp>'+;
' </Totales>'+;
'</Transaccion>'
cSecurity_csec:=c_StrXml(cXmlBody)
cSecurity_csec:=c_Aplicar(ts_token)
//Crear XML para WebService
cXmlBody2:= '' + ;
'<?xml version="1.0" encoding="UTF-8"?>'+;
'<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:tem="http://tempuri.org/">'+;
' <soap:Header xmlns:wsa="http://www.w3.org/2005/08/addressing"><wsa:Action>http://tempuri.org/IWcfCP/Autenticar</wsa:Action><wsa:To>https://zofri.mekanosige.cl:2414/WcfCP.svc</wsa:To></soap:Header>'+;
' <soap:Body>'+;
' <tem:EnviarTransaccion>'+;
' <tem:XmlTrx>'+cXmlBody+'</tem:XmlTrx>'+;
' </tem:EnviarTransaccion>'+;
' </soap:Body>'+;
'</soap:Envelope>'
// ' <tem:XmlTrx>'+c_strxml(cXmlBody)+'</tem:XmlTrx>'+;
ts_servidor:=tw_servidor
doc = CreateObject( "MSXML2.DOMDocument" )
ohttp=CreateObject( "Microsoft.XMLHTTP")
ohttp:Open( "POST" , ts_servidor+' HTTP/1.1',.f. )
ohttp:SetRequestHeader( "Accept-Encoding","gzip,deflate")
ohttp:SetRequestHeader( "Content-Type" , "application/soap+xml;charset=UTF-8" )
ohttp:SetRequestHeader( "SOAPAction" , "http://tempuri.org/IWcfCP/Autenticar" )
ohttp:SetRequestHeader( "User-Agent" , "Jakarta Commons-HttpClient/3.1;Apache-HttpClient/4.1.1 (java 1.5)" )
ohttp:setrequestHeader( "Host", "https://zofri.mekanosige.cl:2414" )
ohttp:SetRequestHeader( "Connection" , "Keep-Alive" )
doc:LoadXML( cXmlBody2)
TRY
ohttp:Send( doc:xml )
CATCH
cErrorMsg:='No Se Pudo Enviar Documento XML...'+CRLF+CRLF+;
'Sin Conexion a Internet o Servidor Con Problemas'
msginfo(cErrorMsg,"Intente Nuevamente")
return .t.
END
cresponse=ohttp:responseText
?cresponse
If cresponse
msginfo('Servicio Ejecutado Correctamente','Transaccion Exitosa')
else
msginfo('Servicio Con Problemas','Transaccion Erronea')
endif
Release doc
Release ohttp
Release res
Release response
RETURN NIL
DLL32 FUNCTION c_Security(rutacert AS STRING, pass AS STRING) AS STRING PASCAL;
FROM "cSecurity" LIB hLib
DLL32 FUNCTION c_Aplicar(token AS STRING) AS BOOL PASCAL;
FROM "AplicarSeguridad" LIB hLib
DLL32 FUNCTION c_StrXml(sendxml AS STRING) AS BOOL PASCAL;
FROM "StrXml" LIB hLib