SBX - Search With Button

SBX - Forum Post Title

How to Send Http post with NAV

Microsoft Dynamics NAV Forum

MobileMachine asked a question on 26 Jun 2014 7:43 PM
My Badges

Question Status

Verified

Hi,

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

Thanks

Reply
Jens Glathe responded on 26 Jun 2014 9:21 PM
My Badges
Suggested Answer

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

Reply
MobileMachine responded on 26 Jun 2014 10:32 PM
My Badges

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

Reply
Jens Glathe responded on 26 Jun 2014 11:14 PM
My Badges
Verified Answer

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

Reply
Kevin Stone responded on 27 Jun 2014 4:43 AM

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.

 }

}

Reply
Romas Leonavicius responded on 7 Aug 2015 2:07 AM

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

Reply
Kevin Stone responded on 7 Aug 2015 6:34 AM

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

Reply
Jens Glathe responded on 26 Jun 2014 11:14 PM
My Badges
Verified Answer

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

Reply
Jens Glathe responded on 26 Jun 2014 9:21 PM
My Badges
Suggested Answer

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

Reply

SBX - Two Col Forum

SBX - Migrated JS