Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics NAV (Archived)

How to Send Http post with NAV

Posted on by Microsoft Employee

Hi,

I need some help to send Http Post. How this can be done in NAV 2013.

Thanks

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to Send Http post with NAV

    Hi Romas,

    I copied the code from above this morning and pasted it into a text file. For some reason when I pasted it originally, it changed a single quote (') to a (&) in the line that ends with soap:Body>&);. Otherwise I imported the code into NAV 2013 and compiled it without issue.

    Do you have the .Net Framework 4.0 installed on your machine? Sometimes a newer version doesn't actually install all of the pieces of the older versions. It is also possible that NAV is fussy in the order in which you declare the variables. The above order was how I called them.

    The 'Str' variable is defined as System.IO.Stream so it should be obvious to the compiler that is the constructor to use. I am using a minor variation of this code in Production so I know it is working.

    Kevin

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to Send Http post with NAV

    Hi Kevin, thank you for code, however I have ambiguous error on line "reader := reader.XmlTextReader(str);" during compilation:

    ---------------------------

    The call is ambiguous between the following methods:

    'XmlTextReader(System.IO.Stream input)'

    and

    'XmlTextReader(System.IO.TextReader input)'

    ---------------------------

    because there are two constructors XmlTextReader with one parameter. How did you solve this issue?

    The same ambiguous error on line "document.Load(reader);"

    Regards

    Romas

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to Send Http post with NAV

    I created a code unit using .Net that uses http post to call web services. I am sure you can adapt it to fit your needs.

    OBJECT Codeunit 50098 Call To Webservice

    {

     OBJECT-PROPERTIES

     {

       Date=06/27/14;

       Time=[ 7:38:56 AM];

       Modified=Yes;

       Version List=;

     }

     PROPERTIES

     {

       OnRun=BEGIN

             END;

     }

     CODE

     {

       VAR

       ;

       PROCEDURE fncSendToWebservice@1000000011(xml@1000000000 : Text[1000];url@1000000001 : Text[1000];soapActionUrl@1000000002 : Text[1000]) : Text[1000];

       VAR

         sb@1000000003 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Text.StringBuilder";

         uriObj@1000000004 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Uri";

         lgRequest@1000000005 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.HttpWebRequest";

         stream@1000000006 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.StreamWriter";

         lgResponse@1000000007 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.HttpWebResponse";

         str@1000000008 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.Stream";

         reader@1000000009 : DotNet "'System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlTextReader";

         document@1000000010 : DotNet "'System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlDocument";

         ascii@1000000011 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Text.Encoding";

         credentials@1000000012 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.CredentialCache";

       BEGIN

         sb := sb.StringBuilder();

         sb.Append('<soap:Envelope xmlns:xsi="www.w3.org/.../XMLSchema-instance" xmlns:xsd="www.w3.org/.../XMLSchema" xmlns:soap="schemas.xmlsoap.org/.../"><soap:Body>&);

         sb.Append(xml);

         sb.Append('</soap:Body></soap:Envelope>');

         uriObj := uriObj.Uri(url);

         lgRequest := lgRequest.CreateDefault(uriObj);

         lgRequest.Method := 'POST';

         lgRequest.ContentType := 'text/xml';

         lgRequest.Headers.Add('SOAPAction', soapActionUrl);

         lgRequest.Credentials := credentials.DefaultCredentials;

         lgRequest.Timeout := 120000;

         stream := stream.StreamWriter(lgRequest.GetRequestStream(), ascii.ASCII);

         stream.Write(sb.ToString());

         stream.Close();

         lgResponse := lgRequest.GetResponse();

         str := lgResponse.GetResponseStream();

         reader := reader.XmlTextReader(str);

         document := document.XmlDocument();

         document.Load(reader);

         reader.Close();

         str.Close();

       END;

       BEGIN

       END.

     }

    }

  • Verified answer
    Jens Glathe Profile Picture
    Jens Glathe 6,092 on at
    RE: How to Send Http post with NAV

    Hi Mobilemachine,

    NAV has no direct interface to send network requests via C/AL. You can use all .net classes, though. A little search has turned up a possible example on how to do this.

    with best regards

    Jens

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to Send Http post with NAV

    Hi Jens,

    Thanks for the reply. I am newbie in NAV. Can use XMLHttpRequest in NAV instead? I was tasked to look into the possibility to provide Push Notification From NAV to Mobile phone VIA Microsoft Push Notification Services. All I need is to send a Http post from NAV. Since Http is out of the question, is XMLHttprequest available to NAV?

    Thanks

  • Suggested answer
    Jens Glathe Profile Picture
    Jens Glathe 6,092 on at
    RE: How to Send Http post with NAV

    Hi Mobilemachine,

    you can't do this directly in C/AL. What you can do is use or write a .net component which does the network handling. Also, COMMANDLINE or SHELL is no longer supported in NAV2013 (R2), so using curl or wget is out.

    with best regards

    Jens

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans