Page 1 of 1

Acceso a webservice para SII

Posted: Wed Apr 05, 2017 6:30 pm
by gmart1
Buenas tardes,
estoy haciendo pruebas para acceder al webservice de hacienda y poder enviar y recibir ficheros XML para el Suministro Inmediato de Información.
Este trozo de código :

Code: Select all

    local oHttp, cXML
    local cUrl := "https://www7.aeat.es/wlpl/SSII-FACT/ws/fe/SiiFactFEV1SOAP"

    cXML := MEMOREAD ( "SIIEmi.XML" )

    oHttp := CreateObject ("MSXML2.XMLHTTP")

    if oHttp = Nil
        ? 'Erro de creacion oHttp'
    endif
    oHttp:Open ( "POST", cUrl, .f. )
    oHttp:SetRequestHeader ( "Content-Type", "text/xml;charset=UTF-8")
    oHttp:SetRequestHeader ( "SOAPAction:", "" )
    oHttp:SetRequestHeader ( "Connection:", "Keep-Alive")
    oHttp:SetRequestHeader ( "Content-length", STR (LEN(cXML) ) )

    try
        oHttp:Send ( cXML )
    catch
        ? oHttp:status
        ? 'Error en Send'
        return .t.
    end
    ? oHttp:responseText
La línea oHttp:status me devuelve 12004
He buscado en internet este número de status y no encuentro que puede ocurrir, ya sé que me falta usar un certificado digital pero no parece que sea lo que me está indicando ese código.

Muchas gracias por la ayuda.

Re: Acceso a webservice para SII

Posted: Wed Apr 05, 2017 7:01 pm
by hmpaquito
Hay que tener instalado un certificado digital valido en el ordenador desde el que se llama al WS de la aeat, sino no funciona, y hay que decirle el nombre al WS.

Y hay que decirselo asi:

Code: Select all

oHttp:SetOption(2,  13056)
oHttp:setOption(3, "C:\Documents and Settings\"tu usuario"\Datos de programa\Microsoft\System Certificates\My\Certificates\XXXXXXXXXXXXXXXXXXXXXXXX") // donde XXXXXXXXXXXXXXXXXXXXXXXX es el nombre de tu certificado.

Aquí hay un buen ejemplo del tema:

http://www.pctoledo.com.br/forum/viewto ... 30#p100267

Re: Acceso a webservice para SII

Posted: Thu Apr 06, 2017 11:41 am
by gmart1
Gracias hmpaquito, he realizado cambios según el enlace que me recomendaste y el código ha quedado así :

Code: Select all

    local oHttp, cXML
    local cUrl := "https://www7.aeat.es"

    try
        oHttp := win_OleCreateObject ("MSXML2.ServerXMLHTTP")   && CreateObject ("MSXML2.XMLHTTP")
    catch
        ? 'Error de creacion oHttp'
        return .t.
    end

    oHttp:SetOption(2, 13056)  
    oHttp:SetOption(3, "C:\Users\apoyo3\AppData\Roaming\Microsoft\SystemCertificates\My\Certificates\BFC7148ACEE713342C714604600C8BEE2417F631" )
    oHttp:Open ( "POST", cUrl, .f. )
    oHttp:SetRequestHeader ( "Content-Type", "text/xml;charset=UTF-8")
    oHttp:SetRequestHeader ( "Connection", "Keep-Alive")
    oHttp:SetRequestHeader ( "SOAPAction", "/wlpl/SSII-FACT/ws/fe/SiiFactFEV1SOAP" )

    cXML := MEMOREAD ( "SIIEmi.XML" )

    try
        oHttp:Send ( cXML )
    catch
        ? oHttp:readyState
        ? 'Error en Send'
        return .t.
    end
    ? oHttp:status
    ? oHttp:ResponseText
return Nil
He tenido que crear el objeto MSXML2.ServerXMLHTTP para poder usar SetOption, no tengo muy claro el motivo, pero de esta forma el programa llega al Send y me da en oHttp:readyState el valor 1.
Cómo puedo saber el motivo de error del Send, hay alguna propiedad de oHttp que lo refleje ?
He intentado oHttp:Status pero da este error :
Error description: (DOS Error -2147352567) WINOLE/1007 El dato necesario para completar esta operación no está disponible todavía. (0x8000000A): msxml3.dll

Re: Acceso a webservice para SII

Posted: Thu Apr 06, 2017 11:56 am
by hmpaquito
gmart1,

Según el ejemplo de Quintás, para obtener la respuesta se utiliza ResponseBody, asi hay que poner:

Code: Select all

    oHttp:WaitForResponse( 500 )
    cRetorno := oHttp:ResponseBody

Obtenido del ejemplo de Quintás.

Code: Select all

     oServer := win_OleCreateObject( "MSXML2.ServerXMLHTTP" )
     IF ::cCertificado != NIL
     //oServer:setOption( 2, oServer:getOption( 2 ) - SXH_SERVER_CERT_IGNORE_CERT_DATE_INVALID )
     oServer:setOption( 3, "CURRENT_USER\MY\" + ::cCertificado )
     ENDIF
     oServer:Open( "POST", ::cWebService, .F. )
     oServer:SetRequestHeader( "SOAPAction", cSoapAction )
     oServer:SetRequestHeader( "Content-Type", "application/soap+xml; charset=utf-8" )
    oServer:Send( ::cXmlSoap )

    oServer:WaitForResponse( 500 )
    cRetorno := oServer:ResponseBody

    msgInfo(cRetorno, "Esto puede ser muy grande")
Salu2

Re: Acceso a webservice para SII

Posted: Fri Apr 07, 2017 9:06 am
by AngelSalom

Re: Acceso a webservice para SII

Posted: Fri Apr 07, 2017 3:43 pm
by gmart1
Gracias Angel, ya había visto este enlace, pero había pensado que podría conseguir hacer la conexión al webservice de hacienda por mi mismo.
Esta claro que mi poca experiencia en este tema y la poca información que dan en hacienda no me va a permitir realizarlo.