Error 500 calling external web service from Navision

LnZLnZ Member Posts: 37
edited 2010-03-09 in NAV Three Tier
Hi I'm trying to call a simple web service that returns the uppercase of a string, but I always get Error 500 Internal Server Error.
Following are the Web Service XML and my Navision code
Any help would be appreciated
LnZ
--
POST /WSNAV/Service.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/UpString"

<?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/">
  <soap:Body>
    <UpString xmlns="http://tempuri.org">
      <toUP>string</toUP>
    </UpString>
  </soap:Body>
</soap:Envelope>
Navision code
baseURL := 'http://localhost/WSNAV/Service.asmx/';
result := FALSE;
URL := baseURL+'UpString';
method := 'UpString';
namespace := 'http://tempuri.org';
parameters := '<toUP>prova</toUP>';
// Create XML Document
CREATE(xmldoc,TRUE,TRUE);
// Create SOAP Envelope
soapEnvelope := xmldoc.createElement('Soap:Envelope');
soapEnvelope.setAttribute('xmlns:Soap', 'http://schemas.xmlsoap.org/soap/envelope/');
xmldoc.appendChild(soapEnvelope);
// Create SOAP Body
soapBody := xmldoc.createElement('Soap:Body');
soapEnvelope.appendChild(soapBody);
// Create Method Element
soapMethod := xmldoc.createElement(method);
soapMethod.setAttribute('xmlns', namespace);
soapBody.appendChild(soapMethod);
// Transfer parameters by loading them into a XML Document and move them
CREATE(parametersXmlDoc,TRUE,TRUE);
parametersXmlDoc.loadXML('<parameters>'+parameters+'</parameters>');
IF parametersXmlDoc.firstChild.hasChildNodes THEN
BEGIN
  WHILE parametersXmlDoc.firstChild.childNodes.length>0 DO
  BEGIN
    node := parametersXmlDoc.firstChild.firstChild;
    node := parametersXmlDoc.firstChild.removeChild(node);
    soapMethod.appendChild(node);
  END;
END;
// Create XMLHTTP and SEND
CREATE(xmlhttp, TRUE, TRUE);
xmlhttp.open('POST', URL, FALSE);
xmlhttp.setRequestHeader('Content-type', 'text/xml; charset=utf-8');
xmlhttp.setRequestHeader('SOAPAction', method);
xmlhttp.send(xmldoc);
// If status is OK - Get Result XML
IF xmlhttp.status=200 THEN
BEGIN
  xmldoc := xmlhttp.responseXML;
  xmldoc.setProperty('SelectionLanguage','XPath');
  xmldoc.setProperty('SelectionNamespaces','xmlns:tns="'+namespace+'"');
  //How I can read the result??
  result := TRUE;
END
ELSE BEGIN
  MESSAGE(FORMAT(xmlhttp.status));
  MESSAGE(FORMAT(xmlhttp.statusText));
  MESSAGE(FORMAT(xmlhttp.responseText));
END;

Answers

  • LnZLnZ Member Posts: 37
    I have seen that if I call a web service without parameters everything works, Still have no idea on what is wrong in the parameters I pass to the web service.
    LnZ
  • freddy.dkfreddy.dk Member, Microsoft Employee Posts: 360
    The way you transfer the parameters seems wrong - the function you are calling is taking a string - that's what you should give it (not an XML structure under <parameters>.

    I have a sample on my blog on how to call NAV Web Services from NAV - maybe that can help

    http://blogs.msdn.com/freddyk/archive/2010/01/22/connecting-to-nav-web-services-from-microsoft-dynamics-nav-2009-sp1.aspx
    Freddy Kristiansen
    Group Program Manager, Client
    Microsoft Dynamics NAV
    http://blogs.msdn.com/freddyk

    The information in this post is provided "AS IS" with no warranties, and confers no rights. This post does not represent the thoughts, intentions, plans or strategies of my employer. It is solely my opinion.
  • LnZLnZ Member Posts: 37
    That's true, looking a the POST method definition of the webservice I have seen this
    POST /WSNAV/Service.asmx/UpString HTTP/1.1
    Host: localhost
    Content-Type: application/x-www-form-urlencoded
    Content-Length: length
    
    toUP=string
    

    But I' dont know how to pass this parameter as string, sorry but I'm a newbie ath this type of things
    Thank You
    LnZ
  • LnZLnZ Member Posts: 37
    Hi,
    I have changed my code to pass parameters as string but I still get the same error,If I copy and paste my generated xml into SoapUI it works fine.
    It seems a client problem of my navision function.
    The exact error I get is this Error 500: System.InvalidOperationException: Request format is invalid
    This is my xml for the call:
    <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
    <soap:Header/>
    <soap:Body>
    <UpString xmlns="http://tempuri.org">
    <toUP>prova</toUP>
    </UpString>
    </soap:Body>
    </soap:Envelope>
    

    And this is my new code:
    baseURL := 'http://localhost/WSNAV/Service.asmx/';
    result := FALSE;
    URL := baseURL+'UpString';
    method := 'UpString';
    namespace := 'http://tempuri.org';
    // Create XML Document
    CREATE(xmldoc,TRUE,TRUE);
    // Create SOAP Envelope
    soapEnvelope := xmldoc.createElement('soap:Envelope');
    soapEnvelope.setAttribute('xmlns:soap', 'http://www.w3.org/2003/05/soap-envelope');
    xmldoc.appendChild(soapEnvelope);
    soapHeader := xmldoc.createElement('soap:Header');
    soapEnvelope.appendChild(soapHeader);
    // Create SOAP Body
    soapBody := xmldoc.createElement('soap:Body');
    soapParameters:= xmldoc.createElement('toUP');
    soapParameters.text := 'prova';
    soapEnvelope.appendChild(soapBody);
    // Create Method Element
    soapMethod := xmldoc.createElement(method);
    soapMethod.setAttribute('xmlns', namespace);
    soapMethod.appendChild(soapParameters);
    soapBody.appendChild(soapMethod);
    // Transfer parameters by loading them into a XML Document and move them
    // Create XMLHTTP and SEND
    CREATE(xmlhttp, TRUE, TRUE);
    xmlhttp.open('POST', URL, FALSE);
    
    xmlhttp.setRequestHeader('Content-type', 'application/soap+xml; charset=UTF-8');
    xmlhttp.setRequestHeader('Content-Length',FORMAT(STRLEN(xmldoc.xml)));
    MESSAGE(FORMAT(STRLEN(xmldoc.xml)));
    xmlhttp.setRequestHeader('SOAPAction', method);
    // If status is OK - Get Result XML
    IF xmlhttp.status=200 THEN
    BEGIN
      xmldoc := xmlhttp.responseXML;
      xmldoc.setProperty('SelectionLanguage','XPath');
      xmldoc.setProperty('SelectionNamespaces','xmlns:tns="'+namespace+'"');
      result := TRUE;
    END
    ELSE BEGIN
      MESSAGE(FORMAT(xmlhttp.status));
      MESSAGE(FORMAT(xmlhttp.statusText));
      MESSAGE(FORMAT(xmlhttp.responseText));
    END;
    

    Any Idea?
    LnZ
  • LnZLnZ Member Posts: 37
    Solved, it was a problem on the soap definition, the other developer chganged the namespace without advising me.
Sign In or Register to comment.