Options

Connect Navision with external web service

icapillaicapilla Member Posts: 30
edited 2013-09-04 in NAV Three Tier
hello,

I'm trying to make a call to a webservice from Navision 2009 R2 but I'm having many difficulties.
Webservice called GrabaServicios, and is within the web "http://www.asmred.com/WebSrvs/b2b.asmx"

the code that I have done is as follows:


IF ISCLEAR(XmlHttp) THEN
CREATE(XmlHttp);
XmlHttp.open('POST','http://www.asmred.com/WebSrvs/b2b.asmx',0);
XmlHttp.setRequestHeader('Content-type','text/xml; charset=utf-8');
XmlHttp.setRequestHeader('SOAPAction','http://www.asmred.com/GrabaServicios');
SLEEP(200);
XmlHttp.send(
'<?xml version="1.0" encoding="utf-8"?>'+
'<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'+
'xmlns:xsd="http://www.w3.org/2001/XMLSchema&quot; xmlns:soap="http:/'+
' <soap:Body>'+
' <GrabaServiciosResponse xmlns="http://www.asmred.com/">'+
' <GrabaServiciosResult>'+
' <Servicios uidcliente="2BB1F227-6ECD-420B-9217-CB006F6787B3">'+
' <Envio codbarras="31047620000011">'+
' <Fecha>2012-01-30T17:02:28</Fecha>'+
' <FechaPrevistaEntrega>2012-01-31T00:00:00</FechaPrevistaEntrega>'+
' <Servicio>1</Servicio>'+
' <Horario>3</Horario>'+
' <Bultos>1</Bultos>'+
' <Peso>0.0</Peso>'+
' <Retorno>1</Retorno>'+
' <Destinatario>'+
' <Nombre>qqq qqqq qqq</Nombre>'+
' <Direccion>aaaa aaaa aaaa</Direccion>'+
' <Poblacion>kkkk kk</Poblacion>'+
' <Provincia>sss sss</Provincia>'+
' <Pais>34</Pais>'+
' <CP>07871</CP>'+
' <Telefono>5455444</Telefono>'+
' <Movil />'+
' <Observaciones>aaaaaa</Observaciones>'+
' </Destinatario>'+
' <Referencias>'+
' <Referencia tipo="0">7007181362</Referencia>'+
' </Referencias>'+
' <Importes>'+
' <Debidos />'+
' <Reembolso>0.0000</Reembolso>'+
' </Importes>'+
' </Envio>'+
' </Servicios>'+
' </GrabaServiciosResult>'+
' </GrabaServiciosResponse>'+
' </soap:Body>'+
'</soap:Envelope>'
);

IF XmlHttp.readyState <> 4 THEN
MESSAGE('XmlHttp Not Ready');

IF ISCLEAR(XmlDoc) THEN
CREATE(XmlDoc);

XmlDoc.load(XmlHttp.responseXML);

IF XmlHttp.status <> 200 THEN
MESSAGE('Http Error ' + ' ' + FORMAT(XmlHttp.status) + ': ' + XmlHttp.statusText);

XmlDoc.save('D:\Respuesta.xml');

I tried to send a xml file but it gives dll error msxml3.dll, which is why I chose to put the entire string in the send function.
This code should return a xml values, but returns nothing. I'm going crazy looking for a solution and not find it.

Comments

  • Options
    JAJJAJ Member Posts: 52
    Hi icapilla,

    I have several examples in order to call a external WS from Navision in Dynamics NAV 2009 R2 version. I based in this fantastic post :http://blogs.msdn.com/b/freddyk/archive/2010/01/22/connecting-to-nav-web-services-from-microsoft-dynamics-nav-2009-sp1.aspx

    This is the code from my general function (InvokeNavWS).

    // Create XMLHTTP and SEND
    CREATE(xmlhttp, TRUE, TRUE);
    xmlhttp.open('POST', pURL, FALSE);
    xmlhttp.setRequestHeader('Content-type', 'text/xml; charset=utf-8');
    xmlhttp.setRequestHeader('SOAPAction', pMethod);
    RutaRQ := TEMPORARYPATH + 'Request' + GetFechaHora + '.xml';
    xmldoc.save(RutaRQ);
    xmlhttp.send(xmldoc);

    // If status is OK - Get Result XML
    RutaRS := TEMPORARYPATH + 'Response' + GetFechaHora + '.xml';
    IF xmlhttp.status=200 THEN BEGIN
    xmldoc := xmlhttp.responseXML;
    xmldoc.setProperty('SelectionLanguage', 'XPath');
    xmldoc.setProperty('SelectionNamespaces', 'xmlns:tns="' + pNameSpace + '"');

    //Save Response
    xmldoc.save(RutaRS);
    //BigResponse.ADDTEXT(xmlhttp.responseText);
    //SaveResponse(BigResponse);

    IF pReturnTag <> '' THEN
    pNodeList := xmldoc.selectNodes('//tns:' + pReturnTag);

    result := TRUE;
    END ELSE BEGIN
    xmldoc := xmlhttp.responseXML;

    //Save Response
    xmldoc.save(RutaRS);
    //BigResponse.ADDTEXT(xmlhttp.responseText);
    //SaveResponse(BigResponse);

    xmldoc.setProperty('SelectionLanguage', 'XPath');
    xmldoc.setProperty('SelectionNamespaces', 'xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"');

    pNodeList := xmldoc.selectNodes('//s:' + 'Fault');
    result := FALSE;
    END;


    Regards.
  • Options
    icapillaicapilla Member Posts: 30
    JAJ good morning!, And above all thank you very much for answering so fast.

    I have a question, I think my problem is with the call to web services.

    The three most important lines of this code are:


    Xmlhttp.open ('POST', 'http://www.asmred.com/WebSrvs/b2b.asmx', FALSE);
    XmlHttp.setRequestHeader ('Content-type', 'text / xml; charset = utf-8');
    XmlHttp.setRequestHeader ('SOAPAction', 'http://www.asmred.com/GrabaServicios');

    In the first line, I can take the url and insert it in an internet explorer and takes me to the page corresponding to a set of web services, but instead n the third, not a mistake or if so, the function which I call queiro called "GravaServicio" if I enter this url in internet explorer, does not lead anywhere, so if you can not be wrong ...

    I may be missing some kind of hotfix, or the automation utulizando is incorrect, at this time use the following:

    XmlHttp -> 'Microsoft XML, v6.0'. XMLHTTP '
    XmlDoc -> 'Microsoft XML, v6.0'. DOMDocument '

    Tine do not know if anything to do, but some sites I have come to see the following:

    Xmlhttp.open ('POST', 'http://www.asmred.com/WebSrvs/b2b.asmx', FALSE, 'User', 'Pass');

    Maybe it's this ... in any case I also tried getting the same error http, the wonderful Error 400: BadRequest.
  • Options
    JAJJAJ Member Posts: 52
    Hi icapilla,

    I think that the correct call is (more or less):

    Xmlhttp.open ('POST', 'http://www.asmred.com/WebSrvs/b2b.asmx?op=GrabaServicios', FALSE);
    XmlHttp.setRequestHeader ('Content-type', 'text/xml; charset = utf-8');
    XmlHttp.setRequestHeader ('SOAPAction', 'GrabaServicios');


    Let mi know if is correct.

    Regards and good luck.
  • Options
    icapillaicapilla Member Posts: 30
    Look, it seems that some progress has been made, now generates a reply with the following error:


    System.Web.Services.Protocols.SoapException: Server did not Recognize the value of HTTP Header SOAPAction: GrabaServicios.

    The error status is 500: Internal server error, the error is no longer 400.

    While making the call now, you do not see concrete webservices, seeing this, it may be because it has several versions, and somehow I have to say the specific version I throw?

    The truth is the first time I'm doing this and I are removing years of life.
  • Options
    JAJJAJ Member Posts: 52
    icapilla wrote:
    Look, it seems that some progress has been made, now generates a reply with the following error:


    System.Web.Services.Protocols.SoapException: Server did not Recognize the value of HTTP Header SOAPAction: GrabaServicios.

    The error status is 500: Internal server error, the error is no longer 400.

    While making the call now, you do not see concrete webservices, seeing this, it may be because it has several versions, and somehow I have to say the specific version I throw?

    The truth is the first time I'm doing this and I are removing years of life.


    Hi, try with this:

    Xmlhttp.open ('POST', 'http://www.asmred.com/WebSrvs/b2b.asmx?op=GrabaServicios', FALSE);
    XmlHttp.setRequestHeader ('Content-type', 'text/xml; charset = utf-8');
    XmlHttp.setRequestHeader ('SOAPAction', 'http://www.asmred.com/GrabaServicios');

    regards.
  • Options
    icapillaicapilla Member Posts: 30
    This SOAPAction does not generate the return xml and returns a status 400 Bad request.
    with how easy it is seen in the examples, do not know why is taking so much problems.

    Don't know what else I can try ...
  • Options
    R023R023 Member Posts: 6
    Hi Everybody, first of all, sorry to use this old topic.

    I have the same error, i´m triying to call a webservice from nav and i don´t know how to make it work.

    can anybody tell me any solution? i tried the code posted in this topic and nothing Works.
  • Options
    sl1200sl1200 Member Posts: 1
    Hi everybody.

    If WebService parameter is typeof string, add <![CDATA]<xml string>]]>

    http://www.codigonexo.com/blog/cajon-de ... d-request/

    locautXmlHttp.send(
    '<?xml version="1.0" encoding="utf-8"?>'+
    '<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/"&gt; '+
    ' <soap:Body>'+
    ' <syncIncidents xmlns="http://company.crm.org/">'+
    ' <strXML>'+
    ' <![CDATA[<Params>'+
    ' <crmGuid>"2BB1F227-6ECD-420B-9217-CB006F6787B3"</crmGuid>'+
    ' <codBarras>"31047620000011"</codBarras>'+
    ' </Params>]]>'+
    ' </strXML>'+
    ' </syncIncidents>'+
    ' </soap:Body>'+
    '</soap:Envelope>'
    );


    Regards,
Sign In or Register to comment.